generics - Setter for field is removed by type projection -
i have following sscce:
class foo(val bars: map<int, bar<*>>) { fun <baz> qux(baz: baz) { val bar2 = bars[2]!! bar2.bazes += baz } interface bar<baz> { var bazes: mutablelist<baz> } }
this seems fine me, compiler complains that:
error:(5, 9) kotlin: setter 'bazes' removed type projection
i have no idea means, less how correct it. what's going on here , how around it?
there's couple little issues. temporarily using bars[2]!! bar<baz>
,
w: (4, 20): unchecked cast: foo.bar<any?> foo.bar<baz> e: (5, 20): assignment operators ambiguity: public operator fun <t> collection<baz>.plus(element: baz): list<baz> defined in kotlin.collections @inlineonly public operator inline fun <t> mutablecollection<in baz>.plusassign(element: baz): unit defined in kotlin.collections
kotlin doesn't know whether handle bar2.bazes = bar2.bazes.plus(baz)
or bar2.bazes.plusassign(baz)
. if change var2.bazes.add(baz)
or val bazes
ambiguity goes away.
fixing , removing unsafe cast brings up
e: (5, 20): out-projected type 'mutablelist<out any?>' prohibits use of 'public abstract fun add(element: e): boolean defined in kotlin.collections.mutablelist'
the question of can safely done type projection. gets treated out any?
, can read list, in nothing
, means can't add list.
it's unclear example why you're using *
. if doesn't cause other issues, perhaps lift out <baz>
parameter, e.g.
class foo<baz>(val bars: map<int, bar<baz>>)
Comments
Post a Comment