nimtiazm
04/19/2017, 2:58 PMconst val list = ArrayList<..>()
and list.add(..) shouldn’t be possiblemg6maciej
04/19/2017, 3:06 PMval list: List<...> = ArrayList<..>()
list.add(..)
is not possible.mg6maciej
04/19/2017, 3:07 PMnimtiazm
04/19/2017, 3:09 PMval list = ArrayList<Int>()
println(list.count())
list.add(1)
println(list.count())
nimtiazm
04/19/2017, 3:10 PMmg6maciej
04/19/2017, 3:11 PMval list: List<Int> = ArrayList<Int>()
nimtiazm
04/19/2017, 3:11 PMnimtiazm
04/19/2017, 3:12 PMmg6maciej
04/19/2017, 3:14 PMArrayList
, then you clearly want to mutate it. If you want a list that does not allow mutation, use a List
interface there. You specifically mentioned function arguments in your original proposal. Use List<Int>
as function argument type and you may pass ArrayList<Int>
there. You will not be allowed (unless you cast) to call add
in that function.nimtiazm
04/19/2017, 3:15 PMnimtiazm
04/19/2017, 3:16 PMval
ref, compiler would be able to stop youmg6maciej
04/19/2017, 3:17 PMnimtiazm
04/19/2017, 3:18 PMnimtiazm
04/19/2017, 3:19 PMmg6maciej
04/19/2017, 3:24 PMconst
to arguments have no meaning. Arguments cannot be reassigned and const
has different meaning in language.
const val myConst = 5
will be inlined across the bytecode. That's the meaning of const
.nimtiazm
04/19/2017, 3:28 PMkingsley
04/20/2017, 6:47 AMval list = listOf<Int>()
which solves your problem with type inference and mutability