amit
04/11/2020, 2:05 PMShawn
04/11/2020, 2:38 PMage1
is typed as Any
instead of Hero
- you’re supplying an Int as a default for a get
that returns Hero?
Shawn
04/11/2020, 2:38 PMShawn
04/11/2020, 2:38 PMShawn
04/11/2020, 2:40 PMamit
04/11/2020, 2:52 PMamit
04/11/2020, 2:54 PMamit
04/11/2020, 2:56 PMShawn
04/11/2020, 3:27 PMMap<K, V>
, it’s Map<K, out V>
. When calling .getOrElse()
with a lambda that returns a type that isn’t V
or a subtype, the type system infers that you intend to operate on a map where K
is the same, but V
is now a supertype of the original map’s V
, since it’s a covariant type parameterShawn
04/11/2020, 3:27 PMV
is resolved to be the only thing it can be, which is Any
Shawn
04/11/2020, 3:28 PMmapByName.getOrElse<String, Any>("unknown") { 0 }
IntelliJ will gray out the parameters and indicate that they are redundant since this is what the type system has already resolvedShawn
04/11/2020, 3:29 PMval superMap: Map<String, Any> = mapByName
Shawn
04/11/2020, 3:29 PMage1
as Hero
, or change the invocation to .getOrDefault<String, Hero>()
then the type checker will raise an erroramit
04/11/2020, 3:30 PMShawn
04/11/2020, 3:30 PMamit
04/11/2020, 3:31 PM