``` println("getMember($name)") wh...
# announcements
d
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
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
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
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
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
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
        }
    }