https://kotlinlang.org logo
Title
s

Smallville7123

04/15/2019, 2:49 PM
does kotlin have its own version of
Cloneable
as i cant find it in the docs nor in the search thing at kotlinlang.org
r

ribesg

04/15/2019, 2:50 PM
Not sure what
Cloneable
is but every
data class
has a
copy
method
f

fred.deschenes

04/15/2019, 2:51 PM
you can just use the one from
java.lang
s

Smallville7123

04/15/2019, 2:51 PM
Clonable supposidly provides the
.clone()
method in alot of java classes
i cant access java in kotlin-native
and
kotlin.Cloneable
does not exist
f

fred.deschenes

04/15/2019, 2:52 PM
well java.lang.Cloneable is an empty interface anyway so just create your own I guess
k

karelpeeters

04/15/2019, 2:52 PM
You should try to avoid in Java too, it's a mess.
☝️ 4
s

Smallville7123

04/15/2019, 2:54 PM
ok
btw, if i have
var x = 1
fun a() {
    var y = x
    y = 6
    println(x)
    println(y)
}
a()
shouldnt x be modified such that y and x have the same value?
k

karelpeeters

04/15/2019, 2:56 PM
No, that's not how variables work in most programming languages.
You can think of variables as boxes, and
x = y
means "take the value that's in
y
and put it in
x
too". The boxes themselves,
x
and
y
, stay independent.
s

Smallville7123

04/15/2019, 2:58 PM
ok, so
y
would obtain the value of
x
and not the reference to
x
itself?
k

karelpeeters

04/15/2019, 2:58 PM
Yes.
s

Smallville7123

04/15/2019, 2:58 PM
ok
so to implement
clone()
i would do this?
fun clone(): LinkedList<T> {
        val tmp = LinkedList<T>()
        tmp.head = head
        return tmp
    }
if so would
fun clone(): LinkedList<T> = LinkedList<T>().head = head
also work?
k

karelpeeters

04/15/2019, 3:02 PM
Assignments aren't expressions in Kotlin, so the second won't work and wouldn't do what you wanted anyway.
d

dreamreal

04/15/2019, 3:03 PM
he doesn't understand kotlin much, unfortunately - he's been spamming other networks for weeks with the same stuff
k

karelpeeters

04/15/2019, 3:03 PM
The first one appears to make a shallow clone, ie. if you mutate the clone the original will change too.
s

Smallville7123

04/15/2019, 3:07 PM
so i would need this?
val tmp = LinkedList<T>()
        forEach { tmp.append(it!!) }
        return tmp
k

karelpeeters

04/15/2019, 3:08 PM
Not sure why you've got the
!!
, but yeah.
s

Smallville7123

04/15/2019, 3:08 PM
ok
can lists normally accept null values as valid elements?
eg MutableList<String?>().add(null)
k

karelpeeters

04/15/2019, 3:10 PM
They take a type parameter
T
, and users can make it nullable if they want, like in your example
String?
. The list code itself doesn't need to concern itself with nullability.
s

Smallville7123

04/15/2019, 3:10 PM
ok