Is there anything in kotlin syntax that holds it b...
# announcements
h
Is there anything in kotlin syntax that holds it back from adding python's swapping syntax?
Copy code
a, b = b, a
the only place I can think of where commas are not enclosed in
()
is
enum
classes. would that case alone cause python swap syntax to turn kotlin into an irregular language or something? are free-range commas just too dangerous for future expansion? could they not use the same solution as in
enum
and require a
;
for only the second time in the language, and thus open up all sorts of destructuring assignment, like that which you see in javascript?
n
Unfortunately kotlin decided to remove tuples
which I don't reallya gree with
you can add them back yourself, and do this:
Copy code
a, b = makeTuple(b, a)
h
when did it remove them?
i wasn't aware kotlin ever had tuples in the way that python or c# has them. so do you mean stuff like
Copy code
val (a, b, c) = elements
because that still exists
n
well, kotlin has destructuring
it doesn't have built in tuples, but youcan simply write data classesl ike this
Copy code
data class Tuple1<T>(val first: T)
data class Tuple2<T, U>(val first: T, val second: U)
and so on
h
it has destructuring, just not as flexible as js:
Copy code
a, b = (c, ...d)
n
because of overloading this is actually more useful than it seems:
Copy code
fun<T> makeTuple(first: T) = Tuple1(first)
fun<T, U> makeTuple(first: T, second: U) = Tuple2(first, second)
so once you do something like that, for some reasonable number (up to 9), hten you can do what you wrote
it can't be as flexible as JS or python because those languages are dynamic to begin with, so their tuples have dynamic extent
effectively
h
typescript isn't dynamic iirc and it has that kind of destructuring
n
I admit i'm not familiar with how TS handles it. TS is also a static type system bolted on top of a dynamic language. It's pretty much teh same as mypy + python.
1
2
with python and mypy, there are some super dynamic things like that where the type system either complains or cops out and doesn't check it
static type systems will never do that by default
(statically typed languages I mean)
h
also you can do a kind of swapping in kotlin using destructuring but you'd have to wrap everything up in a data class, e.g. your tuple, when really i'm just wanting some syntactic sugar for:
Copy code
val tmp = a
a = b
b = a
n
well you only have to write the dataclasses once
that's basically what kotlin "tuples" were
IMHO it would make sense to add back tuples, before adding a special feature for juts swapping and nothing else.
Kotlin suffers from this in other places too. zip is a good example.
h
and if you have to write the code to instantiate that tuple just to destructure it, you're not really saving any characters there <_<
you're referring to
Iterable.zip(Iterable)
?
n
well, you're avoiding creating a temporary which is pretty annoying
Yes
you can also do
Copy code
a, b = run { Pair(b, a) }
I think
Kotlin only really zips two iterables nicely, after that it starts nesting things
in python you can do
for a, b, c in zip(x, y, z)
In kotlin you can do that too but you need to write out those same boilerplate-y tuples, once 🙂 And you of course also need to write a bit of boilerplate-y zip functions
h
kotlin you can't really do destructuring assignment, only destructuring initialization
n
ah that's a good point
h
so you can't do
Copy code
a, b = elements
you can only do
Copy code
val (a, b) = elements
n
Right
I think that would be a reasonable extension
h
in which case, you can't really ever do swap syntax
even with
run
n
right
h
free the comma!