https://kotlinlang.org logo
Title
z

zucen.co

04/19/2018, 9:31 AM
i want to use when expression in this kind of situation
when(string){
   null -> ...
   this.length>1 -> ...
   else ->
}
i resorted to this:
when{
   this==null -> ...
   this.length>1 -> ...
   else ->
}
is this idiomatic solution?
:yes: 1
m

menegatti

04/19/2018, 9:33 AM
btw, you can escape your code with 3 backticks (```) to make it look better
z

zucen.co

04/19/2018, 9:43 AM
ok I will from now 🙂
a

andyb

04/19/2018, 10:14 AM
when{
   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 ->
}
z

zucen.co

04/19/2018, 10:20 AM
ah,, no this is not a probem I was more wondering if I may call attrbute on the argument when(arg) in what i symbolicaly wrote as 'this.length'
a

andyb

04/19/2018, 10:28 AM
Think I misunderstood the question, unfortunately
this
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 sense
z

zucen.co

04/19/2018, 10:43 AM
Ah... really aha... think is ... I was using this construct in special context
fun 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 target
a

andyb

04/19/2018, 10:44 AM
That's right
z

zucen.co

04/19/2018, 10:44 AM
So is the way I worte it, good way?
a

andyb

04/19/2018, 10:44 AM
that should be fine, I'd just compare it to Nothing rather than null though
z

zucen.co

04/19/2018, 10:45 AM
aha.. I will look in dept at Nothing... it is a bit new concept for me.. that Any vs Nothing and Nothing vs void 🙂
a

andyb

04/19/2018, 10:49 AM
You might find this blog post from @natpryce helpful: http://natpryce.com/articles/000818.html
z

zucen.co

04/19/2018, 11:01 AM
Thanks Andy
a

Andreas Sinz

04/19/2018, 11:07 AM
@andyb how do you compare it to Nothing?
a

andyb

04/19/2018, 11:13 AM
this is Nothing
a

Andreas Sinz

04/19/2018, 11:19 AM
but Nothing can be
Nothing
? (pun not intended)
null is Nothing
results in
false
a

andyb

04/19/2018, 11:50 AM
val test : String? = null

    when (test) {
        is Nothing -> println("I am Nothing")
        else -> println("I am $test.")
    }
This code prints out ""I am Nothing"
Interestingly this code is different & prints out "I am null" which indicates that it has gone into the
else
condition: val test : String? = null when { test is Nothing -> println("I am Nothing") else -> println("I populated $test.") }
That has really caught me out & it looks like it might be a bug
z

zucen.co

04/19/2018, 1:22 PM
PLS 🙂 when you find out... if you will put it here 😄
But one more difference this compiles
val 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")
}
a

Andreas Sinz

04/19/2018, 6:42 PM
@andyb
test is Nothing == false
is a bug?