https://kotlinlang.org logo
Title
e

elect

04/21/2023, 1:44 PM
The new assign operator is undocumented yet, but it looks it's usable https://stackoverflow.com/questions/76022932/how-can-i-define-a-custom-assign-operator-overload-in-kotlin
y

Youssef Shoaib [MOD]

04/21/2023, 1:58 PM
I've been looking into it as well. Currently it is quite limited though (it only works on non-local properties). See also: https://kotlinlang.slack.com/archives/CQ3GFJTU1/p1681573479424889?thread_ts=1681564006.929359&cid=CQ3GFJTU1
v

vanshg

04/21/2023, 5:31 PM
I am just curious from an outsider perspective -- what is your specific use case for this in practice? (I realize what it enables mechanically, but what paradigms does it actually make easier?)
y

Youssef Shoaib [MOD]

04/21/2023, 5:39 PM
For kotlin-graphics/glm we basically have these Vector types like Vec3 (which has 3 vars of type float). For graphics, it's very common to have those vectors be mutable, and so is the case here. Being able to assign one Vec3 to another is therefore a very natural copy operation. Yes, arguably, you can work around this by using Immutable Vectors and holding them in vars, but for code that will need to run on every frame, it's not the best of ideas. Those Vector types also allow getting multiple values out of a function efficiently. For instance, we have color conversions from RGB to HSV, and the result of such a conversion gets assigned to a passed-in Vector (similar to ref parameters in other languages). TL;DR: we have mutable vector types that should allow simple copying, and using
=
for that copying makes it a lot more fluent