I've given some thought to it and it may be cool t...
# language-proposals
p
I've given some thought to it and it may be cool to add thread safety to this by implicitly creating readonly locks on objects when passed as "const"
a
does that apply to the instance itself, its properties or everything inside the properties?
p
It applies only to the instance itself, as by allowing only readonly operations on it we don't need anything else
a
Two follow up questions: 1. How does the compiler know which is a
readonly
-operation? 2. If you have a
const foo: Foo
, is it ok to pass it into a
fun bar(foo: Foo)
or is it necessary to annotate the parameter with
const
too?
p
1. assignments, sets and methods containing any of those affecting their members 2. yes, to be able to pass a const to a function its signature needs that param to be specified const
oh, as 1. I've mentioned the non-readonly operations 😛
a
What advantage does that have over a read-only interface?
p
We wouldn't need to have 2 implementations of each object we want to be writable or read-only
a normal reference to List would already be treated as MutableList, and a readonly/const one would be what List is now
given Kotlin's smart nature, in many cases no extra keywords would be needed though
for example:
Copy code
val thing = MyThing()
thing.readOnlyOperation()
functionForReadOnly(thing)
the fact that
thing
is const/readonly can be statically proven, so it wouldn't need any extra keyword in its declaration
the function call wouldn't complain
a
if you call a mutating method on a
const thing
, does it throw an exception or is it not allowed (compiler error)?
p
compiler error
a
in your
List
example, how would Variance work with a
const List<T>
?
p
nice question, I still have to think well about generics, but it would be nice if T could be given a mutability modifier so you could have an immutable list of immutable items, an immutable list of mutable items and the same with mutable lists
All that depends a lot on whether we decide to make the immutability totally recursive or just down to 1 level