https://kotlinlang.org logo
Title
d
p

poohbar

05/29/2018, 5:31 PM
Why so confusing
why doesnt
List
just become
ImmutableList
.. why do we need both a "read-only" list and "immutable" list?
d

diesieben07

05/29/2018, 5:32 PM
Because Java and because backwards compatibility.
I would guess.
p

poohbar

05/29/2018, 5:33 PM
I heard people saying many times to not rely on the fact that you can mutate
List
at the moment because in future it might become fully immutable.
d

diesieben07

05/29/2018, 5:34 PM
Well, the thing I linked is only a proposal and it seems it's not very fleshed out or even updated much. So... not sure. 🤷
k

karelpeeters

05/29/2018, 5:35 PM
So that means switch everything over to
immutableListOf()
instead of
listOf()
?
p

poohbar

05/29/2018, 5:40 PM
yeah that would suck
also
immutableListOf()
is too long.. I am against collection literals but we need reasonably short builder methods
k

karelpeeters

05/29/2018, 5:41 PM
Because whenever I use
listOf()
I meant that I wanted an immutable list.
p

poohbar

05/29/2018, 5:48 PM
Exactly my thoughts.
I also don't understand why is it taking so long to add immutable collections. What's the point of a immutable data class when anytime you have a list field you are open to all kinds of shenenigans
Kotlin should have had it since 1.0
b

bissell

05/29/2018, 6:05 PM
I've thought about creating a
pListOf()
builder and pointing it to some Java persistent collections implementation
p

poohbar

05/29/2018, 6:08 PM
Maybe I am too influenced by Rich Hickey but to me, collections are very very important in any language. Without a good safe data manipulation, what's the point? All software does is manipulate data.
b

bissell

05/29/2018, 6:17 PM
You could use the actual Clojure collections if you want, but I imagine they might be limited to Kotlin/JVM: https://github.com/aol/cyclops-react/wiki/Clojure-and-cyclops-collections
Multiplatform concerns have been the main thing keeping me from doing something like that
p

poohbar

05/29/2018, 6:28 PM
I don't care about multiplatform but I think collections is such a cornerstone of a language that they should come from the stdlib.. The principle of least surprise etc..
g

greybird

05/29/2018, 7:15 PM
When you say that you can modify a Kotlin immutable list (returned by listOf for example) you mean only if you access the Java methods via the Java types, rather than the Kotlin methods, right?
k

karelpeeters

05/29/2018, 7:15 PM
Or you can cast it.
p

poohbar

05/29/2018, 7:15 PM
@greybird correct
(listOf(1,2,3) as ArrayList).add(5);
g

greybird

05/29/2018, 7:17 PM
That doesn't bother me so much, because I think I understand that a thin layer over the Java collections has benefits as well. It is certainly the simplest thing to provide, and as long as I stick to the Kotlin types I can get many of the benefits of immutability.
k

karelpeeters

05/29/2018, 7:18 PM
It's still annoying that you can't return the result from
listOf()
to java code and be sure that it's really immutable.
Or you can't have a parameter typed
List
and assume it is immutable, since it may be a
MutableList
from the caller's point of view.
p

poohbar

05/29/2018, 7:20 PM
Yep.. also cue in heaps of people telling newbies how they can learn Kotlin without knowing Java. This is one of very real javaisms hidden below a thin layer of Kotlin just waiting to bite you in the a**
k

karelpeeters

05/29/2018, 7:21 PM
Well the latter is more a "problem" with the way the contracts are specified, but otherwise I agree.
g

greybird

05/29/2018, 7:56 PM
To implement "real" immutable collections for functional style work is a big job, because they are designed to share memory between instances, when adding an element to create a new immutable collection for example. This is what scala's immutable collections do and what is talked about in the Kotlin proposal. I can understand why this is taking a while. But maybe, as a much simpler step, you would be pretty happy if the list returned by listOf, etc, is simply:
java.util.Collections.unmodifiableList(java.util.ArrayList())
so that Java code can't change it?
p

poohbar

05/29/2018, 8:05 PM
First of all, immutable collections have been implemented for JVM several times. You mentioned Scala but there is also Clojure that had it years ago. Also, I don't see a big risk in picking an implementation and then improving/changing it later as the state-of-the-art progresses. The interfaces would remain the same. As for your example, yes I was surprised this is not already the case and I don't understand what is preventing this intermediate solution.
g

greybird

05/29/2018, 8:08 PM
Don't know. The only reason I can think of not to do it, is to avoid an extra object creation. But maybe the JB folks ran into other problems as well.