Skip to content

Instantly share code, notes, and snippets.

@ntedgi
Last active April 8, 2020 06:50
Show Gist options
  • Save ntedgi/56886504ab0e878ac8660d407723c66b to your computer and use it in GitHub Desktop.
Save ntedgi/56886504ab0e878ac8660d407723c66b to your computer and use it in GitHub Desktop.
inline and refied
fun main() {
inline fun File.forEachFileInline(each: (File)->Unit) = listFiles().forEach(each)
fun File.forEachFile(each: (File)->Unit) = listFiles().forEach(each)
//inline
run{
File("XXX").forEachFileInline {
return@run
}
}
run{
File("XXX").forEachFile {
return@run
}
}
run{
File("XXX").listFiles().forEach {
return@run
}
}
//refied
## An inlined function with reified type is not callable from Java code.
https://stackoverflow.com/questions/45949584/how-does-the-reified-keyword-in-kotlin-work
inline fun <reified T> Int.findParentOfType(): T? {
var p = parent
while (p != null && p !is T) {
p = p.parent
}
return p as T?
}
fun <T> Int.findParentOfType(clazz: Class<T>): T? {
var p = parent
while (p != null && !clazz.isInstance(p)) {
p = p.parent
}
@Suppress("UNCHECKED_CAST")
return p as T?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment