Last active
September 2, 2020 06:08
-
-
Save DreadBoy/2e5d8d68f586401d610caf5b5baf5f01 to your computer and use it in GitHub Desktop.
Higher-order functions in 3 different languages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ;( | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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