it's seems like build in types (`Int` etc) copy on...
# announcements
t
it's seems like build in types (
Int
etc) copy on assignment, but classes create a reference on assignment. (see this simple example : https://pl.kotl.in/tTP-Co4SS). If I want to make my own numeric-ish type is there a way to behave the same as
Int
on assignment?
m
Yup, value classes are exactly what you ask for.
t
can't have var type... so no way to do
+=
?
Or is
Int
immutable as well?
m
no, you should be able to do `+=`just fine, you mutate the outer type type, not inner
Yes, `Int`can be viewed as immutable here. Generally speaking primitive types are considered immutable
Copy code
@JvmInline
value class A(val value: Int) {
    operator fun plus(other: A) = A(value + other.value)
}

var x = A(0)
x += A(1)
compiles fine for me
t
hmmm... looks resonable. I'll give that a shot.
my env is having trouble finding
@JvmInline
. It doesn't seem to exist in kotlin.jvm.*
m
This is required only in 1.5 I believe, see the link above
t
ahh.. yeah. I can't upgrade to 1.5 because of a bug in the null
.?
operator...
it's supposed to be fixed in 1.5.30
strange that I'd get the error about needing
JvmInline
in the 1.4.x compiler, but then not actually have it... oh well.
m
Maybe try `inline`not `value`then?
­čĹć 1