one question i’m honestly still thinking on is if ...
# language-proposals
n
one question i’m honestly still thinking on is if there’s any possibility an object could be made immutable just by expressing it like
const val list = ArrayList<..>()
and list.add(..) shouldn’t be possible
m
With
val list: List<...> = ArrayList<..>()
list.add(..)
is not possible.
(from Kotlin code)
n
Copy code
val list = ArrayList<Int>()
	println(list.count())
	list.add(1)
	println(list.count())
is it a WAT! ?
m
Change your first line to
Copy code
val list: List<Int> = ArrayList<Int>()
n
yeah but i use type-inference wherever I can
you see my point? from my perspective it’s broken
m
If you are using
ArrayList
, then you clearly want to mutate it. If you want a list that does not allow mutation, use a
List
interface there. You specifically mentioned function arguments in your original proposal. Use
List<Int>
as function argument type and you may pass
ArrayList<Int>
there. You will not be allowed (unless you cast) to call
add
in that function.
n
yes you have to specify an immutable interface there. what i’m investigating is if there’s any way this could be unified with type-system so somehow you give a hint to compiler about your mutators and compiler could help you wtih that
a straight forward way would be to introduce a language-level @annotation to mark mutating methods and then if you call any of these after specifying
val
ref, compiler would be able to stop you
m
If you are looking for type system that does not allow mutations by default, try Haskell (or eta-lang.org for JVM equivalent).
n
why not if kotlin could have it. if it makes sense to teh community
because i use and love kotlin 🙂 like a lot of others
m
I do use Kotlin extensively too. I'm generally happy with what is it now and apart from few minor things I don't feel it needs to get more complicated. In my eyes you failed to provide explanation to what you exactly want changed and why you want it like that. Adding
const
to arguments have no meaning. Arguments cannot be reassigned and
const
has different meaning in language.
const val myConst = 5
will be inlined across the bytecode. That's the meaning of
const
.
n
at strawman level you never decide if it’s gonna be a new keyword or reusing existing keyword might work it out. you try to keep it minimum so the intent is delivered. I’m not sure if you still understand that i’m talking about two problems and how they are connected.
k
@nimtiazm why not just do
val list = listOf<Int>()
which solves your problem with type inference and mutability