https://kotlinlang.org logo
#announcements
Title
# announcements
d

deinspanjer

05/24/2017, 7:41 PM
Copy code
println("getMember($name)")
        when (name) {
            "toString" -> return { obj.toString() }
            "valueOf" -> return {
                when (obj) {
                    is JsonNull -> null
                    is JsonArray -> obj.toString()
                    is JsonPrimitive ->
                        if (obj.isNumber) obj.asNumber
                        else if (obj.isBoolean) obj.asBoolean
                        else obj.asString
                    else -> obj.toString()
                }
            }
            else -> {
                try {
                    return obj[name!!]
                } catch (e: NoSuchElementException) {
                    return null
                }
            }
        }
p

pawel.barszcz

05/24/2017, 7:44 PM
deinspanjer: Not an answer to your question, but… I would feel safer (about types and
Any
too) if I put one
return
before
when
instead of returning the whole function inside every branch of
when
I mean:
Copy code
return when (...) {
    ... -> someValue
    ... -> when (...) {
          ... -> is ... -> anotherValue
                   else -> yetAnotherValue
so the whole
when
is an expression which evaulates to something and that something is then returned from function
d

deinspanjer

05/24/2017, 7:46 PM
Hmm. I’m trying to implement a ScriptObjectMirror interface for Nashorn here, and when it calls getMember, it is expecting to get a function back, so that is why I was trying to return lambdas.
I see what you mean though..
p

pawel.barszcz

05/24/2017, 7:46 PM
I think that it would be just more, hmm, idiomatic way of writing Kotlin
and then the whole
when
would have some type (propbably
Any
), while now, I guess, it have
Nothing
type (because you are calling
return
inside
when
, and return is terminating the whole function so it;s probably a type
Nothing
)
d

deinspanjer

05/24/2017, 7:48 PM
So maybe something like this?
Copy code
override fun getMember(name: String?): Any? {
        println("getMember($name)")
        return {
            when (name) {
                "toString" -> obj.toString()
                "valueOf" -> when (obj) {
                        is JsonNull -> null
                        is JsonArray -> obj.toString()
                        is JsonPrimitive ->
                            if (obj.isNumber) obj.asNumber
                            else if (obj.isBoolean) obj.asBoolean
                            else obj.asString
                        else -> obj.toString()
                }
                else -> {
                    try {
                        obj[name!!]
                    } catch (e: NoSuchElementException) {
                        null
                    }
                }
            }
        }
    }
p

pawel.barszcz

05/24/2017, 7:49 PM
looks like
wait, I see you have remoed lambdas… I will check in my IDE in a moment
hmm , are you sure that you want to return lambdas instead of values?
eg. that
{ obj.toString() }
returned in
toString
branch
instead of “just”
obj.toString()
as you have written in modified sample above
Ah, and BTW, you are catching
NoSuchElementException
only, when you still can have exception because of calling
!!
on nullable
name
so, summing up, my suggestion is as follows:
Copy code
return when (name) {
        "toString" -> obj.toString()
        "valueOf" -> when (obj) {
            is JsonNull -> null
            is JsonArray -> obj.toString()
            is JsonPrimitive ->
                if (obj.isNumber) obj.asNumber
                else if (obj.isBoolean) obj.asBoolean
                else obj.asString
            else -> obj.toString()
        }
        else -> try {
            obj[name!!]
        } catch (e: Throwable) {
            null
        }
    }
2 Views