0

I want to use nested pair in kotlin, such as "a" to {"b" to "c"}

I have tried :

"teachers" to {"a" to "c"; "c" to "d"}

but when I debug this, the data type is:

 (teachers, () -> kotlin.Pair<kotlin.String, kotlin.String>)

how to use this?

if don't use

"a" to mapOf("a" to "b"...)

Is it possible?

1 Answer 1

4

{ A -> B } is an anonymous function whose parameter is A and body is B.

{ B } is a short form of { () -> B }.

In addition,

A; B

is the same as

A
B

Therefore {"a" to "c"; "c" to "d"} means a function whose parameter list is () (zero parameters) and body is

Pair("a", "c")
Pair("c", "d")

which is equivalent to something like ::noName with

fun noName() {
    Pair("a", "c")
    return Pair("c", "d")
}

Anyway, what are the braces in your code? They do not mean "pairs." I think you meant to represent a map (or dictionary) in python, but PAIRS ARE NOT MAPS and vice versa.

Nested pair is something like this: "a" to ("b" to "c") which is equivalent to Pair("a", Pair("b", "c"))

If you want to make a map in Kotlin, you should use function mapOf().

If you want to make an array of pairs in Kotlin, you can do so like arrayOf("a" to "b", "c" to "d").

Also, arrays are not maps and vice versa.

This is an example of (a pair of (a string) and (an array of (pairs of strings))).

"a" to arrayOf("b" to "c", "d" to "e")
Sign up to request clarification or add additional context in comments.

1 Comment

listOf would be better than arrayOf thou

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.