Why Kotlin use the word “val” to stand for constant?

I really want to know why Kotlin use the word val to stand for constant?

If var means variable.val means what ? val means var + l or val is only one word for short?

In the Kotlin Documentation,we only have the following words.

Read-only local variables are declared using val keyword. Mutable
local variables are declared using var keyword.

I have searched a lot by Google and the forum here, but I found nothing helpful.

First I asked this question at StackOverflow:

Thanks.

1 Like

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).

val is short for value.

2 Likes

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.

2 Likes
class MutableVal{
    val rand = Random()
    val t get() = rand.nextInt()
}

t is a mutable readonly

2 Likes

Because val is short, and you’re going to type it A LOT. :wink:

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).

In Kotlin 1.0 perhaps, but not in Kotlin 1.1 and later with the addition of local delegated properties:

class MutableLocalVal {
    val delegate = object : ReadOnlyProperty<Nothing?, Int>
    {
        val rand = Random()

        override fun getValue(thisRef: Nothing?, property: KProperty<*>) = rand.nextInt()
    }

    fun foo()
    {
        val localVal by delegate

        (1..10).forEach { println(localVal) }
    }
}
1 Like

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 :smiley:

1 Like

It probably is :wink: 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:

  1. https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables
  2. https://kotlinlang.org/docs/reference/properties.html

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.

1 Like

Hi Wasabi , thank you for the response.
I have looked the first link and for variables that was clear for me since the beginning :smiley: , 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.

1 Like

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”.

2 Likes

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.

It could be a compile time type system feature, which is perfectly fine and not unheard of. Kotlin does similar things with generics or NonNull types.

1 Like