zucen.co
04/19/2018, 9:31 AMwhen(string){
null -> ...
this.length>1 -> ...
else ->
}
i resorted to this:
when{
this==null -> ...
this.length>1 -> ...
else ->
}
is this idiomatic solution?menegatti
04/19/2018, 9:33 AMzucen.co
04/19/2018, 9:43 AMandyb
04/19/2018, 10:14 AMwhen{
this is Nothing -> ...
this.length>1 -> ...
else ->
}
Is the action of an empty String the same as a null?
If so you could rewrite it as
when{
this is Nothing || this.length == 0-> ...
else ->
}
zucen.co
04/19/2018, 10:20 AMandyb
04/19/2018, 10:28 AMthis
will not correspond to the subject of the when
condition. To need to evaluate the branches using the name of the object outside the when
block. Hope that makes sensezucen.co
04/19/2018, 10:43 AMfun String?.shortened(maxLen:Int=60) = when{
this == null -> ""
this.length<maxLen -> this
else -> "${this.substring(0,maxLen)}...(+${this.length-maxLen})"
}
so I thought this was in this context reference to when argument... but in fact it is this as extended targetandyb
04/19/2018, 10:44 AMzucen.co
04/19/2018, 10:44 AMandyb
04/19/2018, 10:44 AMzucen.co
04/19/2018, 10:45 AMandyb
04/19/2018, 10:49 AMzucen.co
04/19/2018, 11:01 AMAndreas Sinz
04/19/2018, 11:07 AMandyb
04/19/2018, 11:13 AMAndreas Sinz
04/19/2018, 11:19 AMNothing
? (pun not intended)null is Nothing
results in false
andyb
04/19/2018, 11:50 AMval test : String? = null
when (test) {
is Nothing -> println("I am Nothing")
else -> println("I am $test.")
}
This code prints out ""I am Nothing"else
condition:
val test : String? = null
when {
test is Nothing -> println("I am Nothing")
else -> println("I populated $test.")
}zucen.co
04/19/2018, 1:22 PMval alfa:String?=null
when{
alfa == null -> print("nothing")
alfa.length>1 -> print("${alfa.length}")
else -> print("else")
}
this does not
when{
alfa is Nothing -> print("nothing")
alfa.length>1 -> print("${alfa.length}")
else -> print("else")
}
Andreas Sinz
04/19/2018, 6:42 PMtest is Nothing == false
is a bug?