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?
amit
04/11/2020, 2:52 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 parameterV
is resolved to be the only thing it can be, which is Any
mapByName.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 resolvedval superMap: Map<String, Any> = mapByName
age1
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