A val is not constant; you can get different values at different times. It is just that you cannot set the value, which means it is read-only to you (as the documentation says).
If the variable declared as val is a simple type (int, float, boolean, etc.) val has the effect of const. However, if the variable holds the reference to an object that has sub-object(s), only the reference to the object is const. Variables inside the object and its sub-object can be changed. Hence, things are not constant.
Okay, with this I’m just repeating what jstuyts already said. Just to make sure the point is understood.
However your example with a val property is a different thing than a local variable (or should I say value?). A local val is actually immutable (what doesn’t mean that the referenced is immutable, too).
No sir, you didn’t repeat what jstuyts already said. You pointed out that if the variable hold the reference to an object that has sub-object(s) → only the reference to the object is const.
Maybe this is trivial for more experienced programmers but this really helped me a lot, because I couldn’t understand that from the documentation.
Thanks a lot
It probably is Can you point me to the part of the documentation that didn’t explain this properly. I personally like to go improve the docs where possible, especially if it’s something that isn’t obvious for new programers.
Maybe I missed some other place but I found 2 main parts of the docs talking about this:
I’m not sure how this can really be improved apart form adding a whole new section to the second page. Maybe you have some suggestion.
That said, maybe this is something better explained in a tutorial since this is a basic idea that can be found in many programming languages.
Hi Wasabi , thank you for the response.
I have looked the first link and for variables that was clear for me since the beginning , now when you click in ’ See also Properties And Fields '(the second page you entered) in the beginning it writes → " Properties in Kotlin classes can be declared either as mutable using the var keyword, or as read-only using the val keyword." , this “read-only” part was confusing to me, maybe it would be good to add an simple example like:
val list = arrayListOf(1,2,3,4,5)
list.add(6)
println(list)
And we can see that when it comes to object only the reference is const and not the state of that object.
I think it’s worth mentioning directly on this topic for future readers:
Kotlin does not use val to stand in for const as OP suggests.
Just like how Java does not use final to stand in for const, Kotlin’s val is really the same as Java’s final fields.
Kotlin does have a const keyword that functions similarly to Java’s const keyword. Oddly it isn’t mentioned in the StackOverflow answers to the link OP posted.
EDIT:
IMO, “const” has taken on the meaning of compiler constant or some kind of intrinsically raw value that will never change. Kind of like the other side of the coin to “volatile”.
In the example with MutableVal the inst var cannot be changed at all. The D programming language has true immutable types, see Type Qualifiers - D Programming Language.
Let’s have a look at some sample code using the immutable keyword in D:
immutable(char)[] s = "hello";
s[0] = 'b'; // error, s[] is immutable
class Foo
{
public int bar;
}
Foo foo = new Foo();
foo.bar = 67; // compiles fine
immutable Foo foo = new Foo();
foo.bar = 67; // compiler error, everything inside foo is immutable as well
Note that the variable bar in Foo is not declared immutable inside class Foo. So it can be mutable or immutable which is decided at the time the variable is declared to hold an instance of class Foo.
This can neither be done in Kotlin nor Scala. If I remember right there was a discussion about having this in Kotlin, but the request was declined. Think the reason was that there is no support for that kind of thing in the JVM and adding such a feature on byte code level would cause too much bloat of byte code, making the compiler slow, etc. So for practical reasons not really doable on the JVM.