https://kotlinlang.org logo
#announcements
Title
# announcements
v

vitruvia

04/21/2017, 1:08 PM
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

elizarov

04/21/2017, 1:12 PM
You can store both Int and String in
Collection<Any>
k

kirillrakhman

04/21/2017, 1:13 PM
type inference will automatically take care of that if you write
listOf(1, "foo")
v

vitruvia

04/21/2017, 1:13 PM
Ah, cool. Thanks!
k

kirillrakhman

04/21/2017, 1:13 PM
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

vitruvia

04/21/2017, 1:14 PM
so operations on it will be dynamically typed?
k

kirillrakhman

04/21/2017, 1:15 PM
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

vitruvia

04/21/2017, 1:16 PM
I think i get it
k

kirillrakhman

04/21/2017, 1:17 PM
this really isn't Kotlin specific, it's how statically typed languages work
v

vitruvia

04/21/2017, 1:19 PM
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

kirillrakhman

04/21/2017, 1:20 PM
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

vitruvia

04/21/2017, 1:20 PM
Thanks, I'll read about it, then.
2 Views