jbx
02/04/2017, 7:19 PMif (val notOptional = someOptional) { … }
. Any plans for something similar in Kotlin? (Or does it exist and I've overlooked it?)aimozg
02/04/2017, 7:25 PMlet
, run
, also
, apply
. Your example would be someOptional?.let{nonOptional -> ... }
.benleggiero
02/04/2017, 7:25 PMif (someOptional != null) {
// In here, it's automatically safe-unwrapped
}
benleggiero
02/04/2017, 7:26 PMaimozg
02/04/2017, 7:27 PM!=null
check does not work for `var`s, but you can capture the value with local val varfield = varfield
benleggiero
02/04/2017, 7:27 PMjbx
02/04/2017, 7:46 PMlet
is the closest to Swift’s if let
, just a little less powerful. I think the Kotlin online reference manual should mention let
in the null safety section.Eugenio
02/04/2017, 8:54 PMlet
is not special, it's the magic of smartcastbenleggiero
02/04/2017, 9:17 PMif (someOptional != null)
😛benleggiero
02/04/2017, 9:17 PMmiha-x64
02/04/2017, 9:25 PMmiha-x64
02/04/2017, 9:32 PMbenleggiero
02/04/2017, 10:04 PMlet
is a stdlib feature, and != null
is a language feature 😉miha-x64
02/04/2017, 10:10 PMjbx
02/04/2017, 11:14 PMif (someOptional != null)
doesn’t unwrap, not exactly idealjbx
02/05/2017, 12:00 AMlet
is superior to `if`:
var someOptional : String? = null
fun badFunc() {
someOptional = null
}
fun optionalTest() {
someOptional = "Foo"
if(someOptional != null) {
badFunc() //some idiot added this function
//prints "null". Idiot broke my code!
println(someOptional)
}
someOptional = "Bar"
someOptional.let { someOptional ->
badFunc() //some idiot added this function
//prints "Bar". Idiot proof!
println(someOptional)
}
}
benleggiero
02/05/2017, 12:29 AMcedric
02/05/2017, 4:15 PMlet
some time ago, with a final preference for not overusing its nullable form: http://beust.com/weblog/2016/01/14/a-close-look-at-kotlins-let/janvladimirmostert
02/06/2017, 8:57 AMlet
in cases where I don't want to introduce a new var
/ val
igor.wojda
02/06/2017, 12:15 PMif
expression containc single check. Converting this to let
will not be so nice if(someOptional != null && data != null)
robin
02/06/2017, 12:56 PMinline fun <A, B, T> letNotNull(arg1: A?, arg2: B?, block: (A, B) -> T): T? {
if(arg1 != null && arg2 != null) {
return block(arg1, arg2)
}
return null
}
robin
02/06/2017, 12:56 PMletNotNull(a, b) { a, b ->
println(a.length)
println(b.length)
}
dmitry.petrov
02/06/2017, 1:03 PM?:
+ break/continue/return/throw for the same effect.dmitry.petrov
02/06/2017, 1:04 PMfor (x in xs) {
val y = foo(x) ?: continue
val z = bar(x, y) ?: continue
doStuff(x, y, z)
}
dmitry.petrov
02/06/2017, 1:08 PMrun
+ return@...
for an arbitrary preemptive exit, but yeah, that's not so readable.dmitry.petrov
02/06/2017, 1:10 PMrun b@ {
val x1 = foo(x ?: return@b)
val y1 = bar(x1, y ?: return@b)
doStuff(x1, y1)
}
groostav
02/06/2017, 7:09 PMgroostav
02/06/2017, 7:13 PMNothing
has always been a big deal to them and I didnt really understood why until kotlin
Doing kotlin things like
val something = nullablueValue ?: throw BlamException("fix your params!")
val result = whatevers.map {
it.someExpression() ?: run { log("no good whatever: $it") return; }
}
is both nifty and convenient.jbx
02/06/2017, 7:50 PMval y = foo(x) ?: continue
syntax was not obvious at all. I glossed over that detail in the reference manual. In my head I had imagined ?:
being defined as something like fun <T?> ?:(optional: T?, defaultValue: T): T { ... }
because of the similarity to Swift’s ??
operator. But Swift does not evaluate the RHS as an expression.kirillrakhman
02/07/2017, 8:24 AMinline infix fun <T> T?.elvis(default: () -> T) = if (this != null) this else default()
fun main(args: Array<String>) {
val nullable: String? = null
val nonNullable = nullable elvis { throw Exception() }
}