xun su
11/21/2022, 1:50 PMString
a primitive type ? if so , why I can use lateinit
on it but not on Int
? the doc(https://kotlinlang.org/docs/properties.html#late-initialized-properties-and-variables) says
The type of the property or variable must be non-null, and it must not be a primitive type.I got a error like this
Joffrey
11/21/2022, 1:51 PMString
is not a primitive type, so everything checks out herexun su
11/21/2022, 1:53 PMJoffrey
11/21/2022, 1:55 PMxun su
11/21/2022, 1:58 PMJoffrey
11/21/2022, 1:59 PMxun su
11/21/2022, 2:05 PMvar name = "something"
, so name is a String
and it is primitive, and var someone = new Person()
then someone is not a primitive.Marcus Brito
11/21/2022, 2:06 PMJoffrey
11/21/2022, 2:07 PMfor me , I think if you can declare some without new , then it is primitiveNo, that just means that the language supports literals for that type
Marcus Brito
11/21/2022, 2:07 PMMarcus Brito
11/21/2022, 2:07 PMKevin Del Castillo
11/21/2022, 2:08 PMnew
keywordKlitos Kyriacou
11/21/2022, 2:09 PMMarcus Brito
11/21/2022, 2:09 PMKlitos Kyriacou
11/21/2022, 2:09 PMMarcus Brito
11/21/2022, 2:10 PMByte
, Short
, Int
, Long
, Char
, Float
, Double
, and Boolean
types are represented as JVM primitives if they’re non-nullable, or as boxed (reference) types if they’re nullable.Marcus Brito
11/21/2022, 2:11 PMJoffrey
11/21/2022, 2:11 PMKotlin documentation is confusing as to whether or not Kotlin has primitives types at all.I agree it's a bit strange that you can get compile errors mentioning them, but there is no clear definition of primitive types in the docs. On the one hand, it is kind of an implementation detail on the JVM, but on the other hand it's quite important that the users are aware of them if they can face limitations on some language features like
lateinit
Kristian Nedrevold
11/21/2022, 2:12 PMKlitos Kyriacou
11/21/2022, 2:13 PMconst
values for the "primitive" types (whatever they are) and also for String
. But not for any other type, such as classes. Even though String is a class.Marcus Brito
11/21/2022, 2:14 PMMarcus Brito
11/21/2022, 2:14 PMKristian Nedrevold
11/21/2022, 2:14 PMKevin Del Castillo
11/21/2022, 2:15 PMconst
values are not necessarily linked to primitives in some languages, I think there's even some work to introduce const
valuesMarcus Brito
11/21/2022, 2:16 PMconst
in Kotlin means “value determined at compile time”Joffrey
11/21/2022, 2:16 PMxun su
11/21/2022, 2:17 PMconst five = 5; five.toString()
in js, so I never dive into that before.😅 I have a long way to learn kotlin.Marcus Brito
11/21/2022, 2:21 PMconst
is more akin to Kotlin’s val
, that is, a non-reassignable variable definitionMarcus Brito
11/21/2022, 2:23 PMconst arr = [1, 2];
arr.push(3);
arr[1] = 'x';
console.log(arr);
> [ 1, 'x', 3' ]
Marcus Brito
11/21/2022, 2:24 PMval
)Kevin Del Castillo
11/21/2022, 2:24 PMMarcus Brito
11/21/2022, 2:24 PMconst
— since only values that are fully computed at compile time can be constxun su
11/21/2022, 2:28 PMval
is equal to const
, so the variable arr
above is not const
, right ? cause you may push something into it later. so it can't be computed at compile time.Joffrey
11/21/2022, 2:30 PMused to think@xun su as Marcus just said, Kotlin'sis equal toval
const
val
indeed corresponds to JS's `const`: they define variables that cannot be reassigned. But Kotlin's val
is not the same as Kotlin's const
, because Kotlin's const
requires a value that can be computed at compile timeKristian Nedrevold
11/21/2022, 2:31 PMMarcus Brito
11/21/2022, 2:32 PMKristian Nedrevold
11/21/2022, 2:33 PMKristian Nedrevold
11/21/2022, 2:36 PMKevin Del Castillo
11/21/2022, 2:38 PMMutableList
with val
and still remove and add elements to it, how is that considered read-only?Kristian Nedrevold
11/21/2022, 2:40 PMMarcus Brito
11/21/2022, 2:40 PMval
only means you can’t assign a different value to that name.Joffrey
11/21/2022, 2:42 PMyou can actually declare a MutableList with val and still remove and add elements to it, how is that considered read-only?@Kevin Del Castillo nobody said the list was read-only, only the
val
property is read-only. It's a property that can only be accessed, but not changed. So the property will always point to the same mutable list. It doesn't mean the list itself is read-only. That depends on the list.Kevin Del Castillo
11/21/2022, 2:43 PMMutableStateFlow
, MutableList
, etc. I think I just misunderstood this:
True, but I think the documentation often refers to it as read-only, especially when talking about collections.
Joffrey
11/21/2022, 2:43 PMeven “read-only” is misleading; they’re just not reassignableI don't really find this confusing, you just need to be clear about what things we are referring to. I believe most of the confusion comes from the fact that some people confuse reference variables with the instances that those variables point to.
Kevin Del Castillo
11/21/2022, 2:46 PMJoffrey
11/21/2022, 2:52 PMTrue, but I think the documentation often refers to it as read-only, especially when talking about collectionsRead-only collections are a different beast, but the terminology is the same because the concept is the same. A handle to something can be read-only, but that doesn't mean the thing it handles cannot be changed. So a
val
property is read-only in the sense that you can only read (access) its value, but not set its value (you cannot assign another value to it). Some implementations of val
properties may return arbitrary values, like val x: Int get() = Random.nextInt()
. This property is read-only, you cannot set its value, but each access will give a different value, so it's not constant, nor immutable (this term is not really applicable to a property, but rather to a value)
In the same vein, a variable of type List
is read-only in the sense that it is a handle that only provides read operations from the list instance it is pointing to. Using such a variable, you can get elements from the list, read the size of the list etc., but not add or remove elements from that list. However, this doesn't prevent (in itself) other pieces of code from adding/removing elements from the list if they use a variable of type MutableList
to access that same list instance, so long as the instance itself really is a mutable list.xun su
11/21/2022, 2:52 PM101
, if you declare a variable with 5 , it is immutable. but if you declare a varible with a list, actually you can add a node or remove a node, but you don't wanna do this , so you use a mutable interface to tag it is readonly so if you add or delete node, your IDE will give you a error.Joffrey
11/21/2022, 2:53 PMxun su
11/21/2022, 3:04 PMJoffrey
11/21/2022, 3:06 PMmain
). You could try it by declaring x
outside the main
function:
import kotlin.random.Random
val x: Int get() = Random.nextInt()
fun main() {
println(x)
println(x)
println(x)
}
See it in the playground here: https://pl.kotl.in/oj6JEmo9oxun su
11/21/2022, 3:26 PMJoffrey
11/21/2022, 3:32 PM