example/idiom.unpacking.primi

Summary

Maintainability
Test Coverage
import ._helpers: assert_error

//
// Unpacking items from iterables.
//

//
// ... from a tuple.
//

a, b, c = (0, 1, 2)
assert(a == 0)
assert(b == 1)
assert(c == 2)

_ = "Not enough values to unpack"
assert_error(() => { a, b, c = (0, 1); }, _)

_ = "Not enough values to unpack"
assert_error(() => { a, b = (0, 1, 3); }, _)

//
// ... from a list.
//

a, b, c = [0, 'x', list]
assert(a == 0)
assert(b == 'x')
assert(c == list)

//
// ... from a dict.
// This unpacks dict keys.
//

a, b, c = {'a': 123, true: 456, null: 789}
assert(a == 'a')
assert(b == true)
assert(c == null)

//
// ... from a string (which is also iterable!)
//

a, b, c = 'čáu'
assert(a == 'č')
assert(b == 'á')
assert(c == 'u')