How do I destructure declarations with tuples? At...
# getting-started
s
How do I destructure declarations with tuples? Attempt:
Copy code
val (a, b) = if (s) (true, "actual ") else (false, "deny ")
I mean I guess I could use
listOf
but is there a better way? EDIT: Ohhh
Pair
is what I should be using it seems
s
yes,
Pair
is the idiomatic thing to use. but don't write
Pair(true, "actual ")
but
true to "actual "
to is an infix function returning a instance of Pair 🤓
s
ahhhhh - cool
Thanks
l
It's a matter of style though. It can be argued that
to
should only be used where there is a relation between them (as in a key/value relationship). If the two values are, for example, the width and height of a rectangle,
2.5 to 9.9
doesn't make sense.
3
h
yeah, as the documentation states:
to
is mostly for map literals. I think the
Pair
constructor is fine.
l
The implementation for
to
literally compiles down to a call to
Pair()
anyway, so there is no technical difference other than style.
s
you forgot the most important metric: character count 🤪
to
uses 4 chars (don't forget the 2 spaces),
Pair(, )
uses 8 chars!
l
That's a very fair argument. And ironic, given that my project is a programming language/interactive data manipulation tool that is optimised for conciseness and low character count. In my tool, 4 chars is outrageously long, and is more characters needed than what I'd use to write a function to, say, check if a string is palindromic or not. 🙂 (just an example, because that was something I saw discussed recently)
Yet, I still argue in favour of
Pair(x,y)
. 🙂
s

Ironic

indeed 😄
😄 1
l
I can't help myself but share what a palindrome verification program in less than 4 characters looks like. This is the full thing, and yes, it's not a joke:
≡∘⌽
s
Every sufficiently code golfed program is indistinguishable from magic. I'm impressed by but not inclined to learn this magic system you came up with 😄 👍