Kotlin Listに対してmutableListOf()が出来るのは何故か?

前提

kotlin

1 var test:Boolean = "20"2 //Type mismatch: inferred type is String but Boolean was expected3 var test2:Int ="20"4 //Type mismatch: inferred type is String but Int was expected5 var tests3:String = 206 //The integer literal does not conform to the expected type String

上記3つのエラーは理解できます
1つ目:Boolean型の変数testに文字列「20」を代入しているから
2つ目:Int型の変数test2に文字列「20」を代入しているから
3つ目:String型の変数test3に整数の「20」を代入しているから

発生している問題・エラーメッセージ

kotlin

1var id: List<Int> = mutableListOf(2)2println(id)//2

この例だと、Int型を要素に持つList型変数idに、mutableList型のリスト(中身は[2])を代入しています
しかし、List型とmutableList型は違うと思うのですが、なぜList型にmutableListを代入出来ているのでしょうか。

補足情報(FW/ツールのバージョンなど)

Kotlin 1.9.20

コメントを投稿

0 コメント