Skip to content

Instantly share code, notes, and snippets.

@DreadBoy
Last active September 2, 2020 06:08
Show Gist options
  • Save DreadBoy/2e5d8d68f586401d610caf5b5baf5f01 to your computer and use it in GitHub Desktop.
Save DreadBoy/2e5d8d68f586401d610caf5b5baf5f01 to your computer and use it in GitHub Desktop.
Higher-order functions in 3 different languages
int one() => 1;
T Function() passThrough1<T>(T Function() fun) => () => fun();
T Function(T Function() fun) passThrough2<T>() => (fun) => fun();
void main() async {
final result1 = passThrough1(one)(); // Can infer the type
final result2 = passThrough2()(one); // Can't infer the type ;(
}
fun one(): Int = 1;
fun <T> passThrough1(func: () -> T) = { func() };
fun <T> passThrough2(): (func: () -> T) -> T = { it() };
fun main() {
val result1 = passThrough1(::one)() // Can infer the type
val result2 = passThrough2()(::one) // Can't infer the type ;( also compiler error
}
const one = () => 1;
const passThrough1 = <T extends any>(fun: () => T) => () => fun();
const passThrough2 = <T extends any>() => (fun: () => T) => fun();
const result1 = passThrough1(one)(); // Can infer the type
const result2 = passThrough2()(one); // Can't infer the type ;(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment