Created
April 19, 2021 12:37
-
-
Save davidmigloz/44e18e65ff3bc05c3df0c066e8017391 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: functions
This file contains 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
greet(String name) => print("Hello $name"); | |
greet2(String person1, [String? person2]) { | |
print("Hello $person1"); | |
if (person2 != null) print("Hello $person2"); | |
} | |
String getGreeting({String name = "Dart"}) { | |
return "Hello $name"; | |
} | |
printGreeting(String getGreeting()) { | |
print(getGreeting()); | |
} | |
printGreetings(String getGreeting(int count)) { | |
for (var count = 0; count < 10; count++) { | |
print(getGreeting(count)); | |
} | |
} | |
main() { | |
greet("David"); | |
greet2("One Person"); | |
greet2("One Person", "Another Person"); | |
final greeting1 = getGreeting(); | |
final greeting3 = getGreeting(name: "Flutter"); | |
printGreeting(() => "Hey Jude"); | |
printGreeting(getGreeting); | |
printGreetings((count) => "Hi $count"); | |
final refToFunction = greet; | |
refToFunction("ref"); | |
} |
This file contains 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 greet(name: String) = println("Hello $name") | |
fun greet2(person1: String, person2: String? = null) { | |
println("Hello $person1") | |
person2?.let { println("Hello $it") } | |
} | |
fun getGreeting(name: String = "Dart"): String { | |
return "Hello $name" | |
} | |
fun printGreeting(getGreeting: () -> String) { | |
println(getGreeting()) | |
} | |
fun printGreetings(getGreeting: (count: Int) -> String) { | |
repeat(10) { count -> | |
println(getGreeting(count)) | |
} | |
} | |
fun main() { | |
greet("David") | |
greet2("One Person") | |
greet2("One Person", "Another Person") | |
val greeting1 = getGreeting() | |
val greeting2 = getGreeting("Flutter") | |
val greeting3 = getGreeting(name = "Kotlin") | |
printGreeting { "Hey Jude" } | |
printGreeting(::getGreeting) | |
printGreetings { count: Int -> "Hi $count" } | |
val refToFunction = ::greet | |
refToFunction("ref") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment