APXEOLOG
12/03/2018, 5:09 PMprivate val checkPoints: MutableSet<String> = HashSet()
fun once(action: () -> Unit) {
val stackTrace = Throwable().stackTrace[1]
val key = "${stackTrace.fileName}_${stackTrace.methodName}_${stackTrace.lineNumber}"
if (!checkPoints.contains(key)) {
checkPoints.add(key)
action()
}
}
fun main() {
repeat(100) {
once { print("Say hi!") }
}
}
karelpeeters
12/03/2018, 7:12 PM!checkPoints.contains(key)
is key !in checkPoints
Alan Evans
12/03/2018, 9:40 PMadd
returns a boolean https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/add.html.
if (checkPoints.add(key)) {
action()
}
rnpy
12/03/2018, 10:19 PMThread.currentThread().stackTrace
can give you current stacktrace without creation a Throwable