When creating a collection or a list do you always...
# announcements
v
When creating a collection or a list do you always need to specify the type of whatever is in those structures with <>? (like Collection<Int>). Can't you make a list that has both Int and Strings?
e
You can store both Int and String in
Collection<Any>
k
type inference will automatically take care of that if you write
listOf(1, "foo")
v
Ah, cool. Thanks!
k
of course that's not really in the sense of statically typed languages
whenever you access that collection you will need to do a type check before you can do anything with the item
v
so operations on it will be dynamically typed?
k
no
listOf(1, "foo")[0]
returns
Any
. You can't do any
Int
operations on
Any
so you will have to do a
is Int
check.
v
I think i get it
k
this really isn't Kotlin specific, it's how statically typed languages work
v
Yeah its just that I only know python and there you don't have to do this type of check. So its all kind of new to me
k
in this case you might want to read up on statically typed languages. I doubt that it will take you long to grasp it.
v
Thanks, I'll read about it, then.