harry.singh
09/14/2020, 6:18 PMval 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:
RequiredWhy am I getting this error and how can I resolve it?but foundInt.CompanionInt
nanodeath
09/14/2020, 6:20 PMnanodeath
09/14/2020, 6:21 PMharry.singh
09/14/2020, 6:21 PMyou aren't like, importing Int.Companion or anything?Nope
harry.singh
09/14/2020, 6:21 PMalso when you say Foo, "Foo" isn't Int, is it?It's actually an
Enum typenanodeath
09/14/2020, 6:23 PMval myMap = mapOf(Foo to 3) work?nanodeath
09/14/2020, 6:23 PMharry.singh
09/14/2020, 6:27 PMdoesIt does not allow me to update the map as it is immutablework?val myMap = mapOf(Foo to 3)
nanodeath
09/14/2020, 6:27 PMmutableMapOf(Foo to 3)nanodeath
09/14/2020, 6:27 PMharry.singh
09/14/2020, 6:27 PMharry.singh
09/14/2020, 6:28 PMharry.singh
09/14/2020, 6:28 PMmutableMapOf(Foo to Int)nanodeath
09/14/2020, 6:28 PMharry.singh
09/14/2020, 6:28 PMnanodeath
09/14/2020, 6:29 PMInt doesn't have a companion class because it's Javananodeath
09/14/2020, 6:29 PMmutableMapOf(Foo to Int::class.java) should workharry.singh
09/14/2020, 6:30 PMif you tell IntelliJ to "specify type explicitly", is it still <Foo, Int>?Nope, it gives me the same error. Required
Int.Companion found Intharry.singh
09/14/2020, 6:32 PMIt now says it requiresshould work (edited)mutableMapOf(Foo to Int::class.java)
Class<Int> but found Intnanodeath
09/14/2020, 6:33 PMInt or actual int literals/instancesharry.singh
09/14/2020, 6:35 PMnanodeath
09/14/2020, 6:38 PMval map = mutableMapOf<MyEnum, Int>()
map[MyEnum.WHATEVER] = 3
should definitely workharry.singh
09/14/2020, 6:47 PMval map = mutableMapOf(
MyEnum.INSTANCE1 to Int,
MyEnum.INSTANCE2 to Int,
MyEnum.INSTANCE3 to Int
)
why does it fail in this case?nanodeath
09/14/2020, 6:48 PMInt) 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 objectnanodeath
09/14/2020, 6:49 PMInt with an int literal, i.e. 1, it'd work fineharry.singh
09/14/2020, 6:51 PMInt with 0 in the aforementioned codeharry.singh
09/14/2020, 6:52 PMnanodeath
09/14/2020, 6:59 PMharry.singh
09/14/2020, 7:01 PMMatteo Mirk
09/15/2020, 11:08 AMMyEnum.INSTANCE1 to Int
you’re creating a Pair<MyEnum, Int.Commpanion> but when executing
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-objectsMatteo Mirk
09/15/2020, 11:08 AMnot true at all,, which doesn’t exist becauseInt.Companionis actually an alias for java.lang.IntegerInt
Int.Companion does exist and holds MAX_VALUE and other constants, check Kotlin source code. The problem here was simply a type mismatch.nanodeath
09/15/2020, 2:35 PMMatteo Mirk
09/15/2020, 2:51 PM