0

Given a functor of a generic Any type, how can I cast an explicity named argument? For instance, I have a JsonArray (vertx) that is essentially a type of List<Any>... I can cast to JsonObject using the implicit it, but if I were to name it participant how can I cast as JsonObject?

val participants = conversation
    .getJsonArray("participants")
    .map{ (it as JsonObject)
        it.getInteger("id")
    }

My IDE complains no matter what I throw at it. Is this even possible? Something like:

val participants = conversation
    .getJsonArray("participants")
    .map{ (participant as JsonObject) ->
        participant.getInteger("id")
    }
2
  • Did you try .map{ (it as JsonObject).getInteger("id") }? Commented Jan 12, 2021 at 1:36
  • @gidds Yes, that works, but sometimes I want to name it explicitly like participant to disambiguate nested mapping function its... but (participant as JsonObject) doesnt compile. Commented Jan 12, 2021 at 1:49

1 Answer 1

1

Like @gidds wrote, following is correct:

val participants = conversation
    .getJsonArray("participants")
    .map { 
       (it as JsonObject).getInteger("id")
    }

For explicit arguments you can write like this:

val participants = conversation
    .getJsonArray("participants")
    .map { participant -> 
       (participant as JsonObject).getInteger("id")
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.