Hey all! I'm trying to use a mutable map with inte...
# getting-started
h
Hey all! I'm trying to use a mutable map with integer values like so:
val myMap = mutableMapOf<Foo, Int>()
. When I try to update the map with some integer values(for e.g.
myMap[Foo] = 3
), it is giving me the following error:
Required
Int.Companion
but found
Int
Why am I getting this error and how can I resolve it?
n
that's bizarre. you aren't like, importing Int.Companion or anything?
also when you say Foo, "Foo" isn't Int, is it?
h
you aren't like, importing Int.Companion or anything?
Nope
also when you say Foo, "Foo" isn't Int, is it?
It's actually an
Enum
type
n
does
val myMap = mapOf(Foo to 3)
work?
and if yes...does it have a different type parameters than what you started with?
h
does 
val myMap = mapOf(Foo to 3)
 work?
It does not allow me to update the map as it is immutable
n
well you could just say
mutableMapOf(Foo to 3)
I was more curious in the types
h
whoops, misunderstood what you were asking
yep it works
but it does not work with
mutableMapOf(Foo to Int)
n
if you tell IntelliJ to "specify type explicitly", is it still <Foo, Int>?
h
checking..
n
Int
doesn't have a companion class because it's Java
mutableMapOf(Foo to Int::class.java)
should work
h
if you tell IntelliJ to "specify type explicitly", is it still <Foo, Int>?
Nope, it gives me the same error. Required
Int.Companion
found
Int
mutableMapOf(Foo to Int::class.java)
 should work (edited)
It now says it requires
Class<Int>
but found
Int
n
I'm not clear on exactly what you want -- do you want
Int
or actual int literals/instances
h
I want to map my enum values to int literals/instances
n
okay, so
Copy code
val map = mutableMapOf<MyEnum, Int>()
map[MyEnum.WHATEVER] = 3
should definitely work
h
Alright, this works. I was previously using:
Copy code
val map = mutableMapOf(
    MyEnum.INSTANCE1 to Int,
    MyEnum.INSTANCE2 to Int,
    MyEnum.INSTANCE3 to Int
)
why does it fail in this case?
n
because when you use a class (i.e.
Int
) as a value (i.e. as an expression, like you are here), it automatically replaces it with the companion object, i.e.
Int.Companion
, which doesn't exist because
Int
is actually an alias for java.lang.Integer (kind of), which being Java doesn't have a
Companion
object
👍 1
if you replace
Int
with an int literal, i.e.
1
, it'd work fine
h
yep, it worked when I replaced
Int
with
0
in the aforementioned code
So, why does Kotlin replaces it with an companion object? Does a companion object represents a value of some sort?
n
yeah, it's a singleton instance
👍 1
h
Thanks Max!
👍 1
m
Harry, it doesn’t work because when you declare
Copy code
MyEnum.INSTANCE1 to Int
you’re creating a
Pair<MyEnum, Int.Commpanion>
but when executing
Copy code
myMap[INSTANCE1] = 3
you’re trying to assign an
Int
to that Entry, so the value type mismatches and the compiler tells you. You were simply trying the wrong thing, what you wanted were int literals, as you found out in this thread. Please read about companion objects here: https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects
@nanodeath
Int.Companion
, which doesn’t exist because 
Int
 is actually an alias for java.lang.Integer
not true at all,
Int.Companion
does exist and holds
MAX_VALUE
and other constants, check Kotlin source code. The problem here was simply a type mismatch.
n
@Matteo Mirk I stand corrected 🙂
m
Didn’t want to sound impolite, sorry 😄