Created
April 20, 2021 22:31
-
-
Save davidmigloz/8ef2b55f243b2a0626b0d98f38372639 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: extension functions
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
extension NumberExtensions on String { | |
int parseInt() { | |
return int.parse(this); | |
} | |
} | |
extension ListExtensions<T> on List<T> { | |
List<List<T>> split(int at) { | |
return [sublist(0, at), sublist(at)]; | |
} | |
} | |
void main() { | |
final num = "42".parseInt(); | |
final subLists = [0, 1, 2, 3, 4].split(2); | |
} |
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 String.parseInt(): Int { | |
return this.toInt() | |
} | |
fun <T> List<T>.split(at: Int): List<List<T>> { | |
return listOf(subList(0, at), subList(at, size)) | |
} | |
fun main() { | |
val num = "42".parseInt() | |
val subLists = listOf(0, 1, 2, 3, 4).split(2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment