Nir
07/07/2019, 4:06 PMinterface MyInterface {
}
data class User(val x: String, val y: Int) : MyInterface
inline operator fun <T : MyInterface> T.plus(f: T.() -> T) : T {
return this.f()
}
fun main() {
var n = User(x = "hello", y = 5)
n += { copy(y = 7) }
}
Nir
07/07/2019, 4:10 PMCzar
07/07/2019, 4:22 PMn += { copy(y = 7) }
is better than n = n.copy(y = 7)
. For me it just obscures the logic.Nir
07/07/2019, 4:35 PMNir
07/07/2019, 4:36 PMmyMap += "name" to 5
Nir
07/07/2019, 4:36 PMmyMap = myMap.put("name", 5)
Nir
07/07/2019, 4:37 PMNir
07/07/2019, 4:43 PMemployeeOfTheMonth = employeeOfTheMonth.copy(y=7)
employeeOfTheMonth += { copy(y=7) }
I think the second's quite a bit more readable. And it potentially avoids bugs if you have two variables of the same type.Nir
07/07/2019, 4:43 PM// Intentional or not, hard to say
employeeOfTheMonth = employeeOfTheWeek.copy(y=7)
gildor
07/07/2019, 4:54 PMNir
07/07/2019, 4:59 PMNir
07/07/2019, 5:01 PMNir
07/07/2019, 5:02 PMkarelpeeters
07/07/2019, 9:18 PM+
makes sense, you're literally adding something to the collection. With your data class you're not adding anything, you're just reassigning and that's =
, not +=
.Nir
07/07/2019, 9:45 PMkarelpeeters
07/07/2019, 9:48 PMNir
07/07/2019, 9:57 PMNir
07/07/2019, 9:58 PM+
specifically is greatNir
07/07/2019, 9:58 PMkarelpeeters
07/07/2019, 9:58 PMNir
07/07/2019, 9:59 PMMap
List
etc it's convenient that there's a way to "modify" the object essentially, without listing it twiceNir
07/07/2019, 9:59 PM+=
works the way it does, right?karelpeeters
07/07/2019, 10:01 PM+=
on mutable collections and then it actually mutates the object. "Listing it twice" is only for immutable objects. It's unfortunate that both concepts are mixed together because of operator overloading though.karelpeeters
07/07/2019, 10:02 PM+=
on immutable collections stored in vars.Nir
07/07/2019, 10:17 PMNir
07/07/2019, 10:18 PM+=
shouldn't do what it does in terms of falling back to x = x + y
Nir
07/07/2019, 10:18 PMkarelpeeters
07/07/2019, 10:19 PMBigInteger
etc would be annoying to use otherwise, but I also think it's unfortunate it also applies to list and maps.Nir
07/07/2019, 10:29 PMNir
07/07/2019, 10:29 PMNir
07/07/2019, 10:30 PMNir
07/07/2019, 10:30 PMkarelpeeters
07/07/2019, 10:30 PM+=
for mutable, +
for immutable, and I like consistency so it's fine as-is for me.karelpeeters
07/07/2019, 10:30 PMNir
07/07/2019, 10:41 PMval
using copy
to "modify" itself, is very very similar to a Map
Nir
07/07/2019, 10:41 PM+
to insert itemskarelpeeters
07/07/2019, 10:42 PMNir
07/07/2019, 10:42 PMNir
07/07/2019, 10:42 PM+
Nir
07/07/2019, 10:50 PMx copy=(y=7)
Nir
07/07/2019, 10:50 PMNir
07/07/2019, 10:51 PM+
or whether it's "adding". The point is to have syntactic sugar that lets you take a non-modifiable var
, and rebind it easilykarelpeeters
07/07/2019, 10:52 PMvar user = User(2, 3)
user=.x = 5
which compiles to
user = user.copy(x = 5)
Nir
07/07/2019, 10:52 PMNir
07/07/2019, 11:05 PMNir
07/07/2019, 11:06 PMNir
07/07/2019, 11:06 PMx += "suffix"
vs x = x + "suffix"
karelpeeters
07/07/2019, 11:09 PMbuildString
.Nir
07/07/2019, 11:20 PMx += y
for read-only makes perfect sense for string/map/list but when you want something similar for an immutable data class, you're told that doesn't make sense, or its unclear, or what's the use casekarelpeeters
07/07/2019, 11:21 PM+
really doesn't make sense though.Nir
07/07/2019, 11:21 PMNir
07/07/2019, 11:22 PM%=
. It's the "mod" operator, mod being short for modification 😉Nir
07/07/2019, 11:23 PMNir
07/07/2019, 11:24 PMx %= { copy(x=7) }
could doNir
07/07/2019, 11:24 PMNir
07/07/2019, 11:25 PMNir
07/07/2019, 11:25 PMfoo=
functions, this wouldn't be necessary.Nir
07/07/2019, 11:26 PMx foo=(y = 7)
is an operator call that translates to x = x.foo(y=7)
for any foo.Nir
07/07/2019, 11:27 PMNir
07/07/2019, 11:28 PMx += "suffix"
you could do x concat=("suffix")
Nir
07/07/2019, 11:28 PMcedric
07/18/2019, 6:38 PM