Kotlin and generics, implementing abstract generic class with generic array -


i have following abstract class

abstract class vec2t<t : number>(open var x: t, open var y: t)

implemented

data class vec2(override var x: float, override var y: float) : vec2t<float>(x, y)

so far, works fine

now, i'd similar matrices, @ moment abstract class

abstract class mat2t<t : number>(open var value: array<out vec2t<t>>)

that should implemented by

class mat2(override var value: array<vec2>) : mat2t<float>(value)

but compiler complains on array<vec2>:

error:(8, 32) kotlin: type of 'value' doesn't match type of overridden var-property 'public open var value: array> defined in main.mat.mat2t'

i told:

  • i can't change type of var property when override (but not changing it, overriding subtype.. same thing?)
  • mat2.value = object : vec2t<float>() { ... } not valid, must not case subclass of mat2t<float>

how may overcome these problems?

is there way have abstract generic class mat2t generic array , implement subtype array?

you can accomplish making generic parameter subtype of vec2t instead of subtype of vec2t's generic parameter type (t : number):

abstract class mat2t<t : vec2t<*>>(open var value: list<t>)  class mat2(override var value: list<vec2>) : mat2t<vec2>(value) 

note overriding var value don't need have in abstract class constructor. same applies vec2t. e.g.:

abstract class vec2t<t : number> {     abstract var x: t     abstract var y: t }  class vec2(override var x: float, override var y: float) : vec2t<float>()  abstract class mat2t<t : vec2t<*>> {     abstract var value: list<t> }  class mat2(override var value: list<vec2>) : mat2t<vec2>() 

these abstract classes represented interfaces instead if suits you:

interface vec2t<t : number> {     var x: t     var y: t }  data class vec2(override var x: float, override var y: float) : vec2t<float>  interface mat2t<t : vec2t<*>> {     var value: list<t> }  data class mat2(override var value: list<vec2>) : mat2t<vec2> 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -