galex
05/06/2020, 10:38 AMguard
in Swift? The best we found for now is to implement a few functions of that sort:
fun <A : Any, B : Any> guard(a: A?, b: B?, block: (A, B) -> Unit): Boolean {
return if (a != null && b != null ) {
block(a, b)
true
} else {
false
}
}
This allows us to run code on a few different types and check that the block ran to have an else
part.
What do you think? It there something better out there? I checked Contracts but you can’t imply a list doesn’t contain a null
item, too bad.John O'Reilly
05/06/2020, 10:41 AMJohn O'Reilly
05/06/2020, 10:47 AMgalex
05/06/2020, 11:22 AMFranSoto
05/06/2020, 11:27 AMfun guard(vararg parameters: Any?, block : (Array<out Any>) -> Unit) = if (parameters.all { it != null }) { val safeParameters = parameters as Array<Any>
block(safeParameters)
} else {}
fun main() {
guard("a", 3) { (a, b) ->
println(b)
}
guard("a", null) {
it.forEach(::println)
}
}
galex
05/06/2020, 11:34 AMFranSoto
05/06/2020, 11:35 AMgalex
05/06/2020, 11:47 AMFranSoto
05/06/2020, 11:50 AM