Destructuring Declarations

This Atom is Under Construction

Suppose you must return more than one item from a function, perhaps a result and some information about that result.

The Pair class allows you to return two values:

// Destructuring/Pairs.kt import atomictest.eq fun compute1(input: Int) = if (input > 5) Pair(input * 2, "High") else Pair(input * 2, "Low") fun main(args: Array<String>) { compute1(7) eq Pair(14, "High") compute1(4) eq Pair(8, "Low") val result = compute1(5) result.first eq 10 result.second eq "Low" }

End of sample. See for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.