https://kotlinlang.org logo
n

Nir

12/18/2020, 7:30 PM
also I realize it can probably be done via reflection, but want to avoid that
n

Nikky

12/18/2020, 9:11 PM
issue is that you know you need a
D
but the specific of the instance of
D
used to construct a point are thrown away and only the
dimension
attribute is kept with
reified
you can get the type that was used, but still won't get you the
DimThree
object i currently see no way around restructuring this or storing the dimension.. or at the least
D::dimension
from what i can tell the length of
data
is always
D::dimension
and everything should be fine as long as that matches.. maybe that is good enough ?
n

Nir

12/18/2020, 10:02 PM
hmm, you gave me an idea, I think it can be done with another private constructor, perhaps
not a real solution of course but a workaround
Copy code
data class Point<D : Dimension> private constructor(val data: List<Int>) {
    constructor(d: D, init: (Int) -> Int) : this(List(d.dimension, init))
    operator fun <D : Dimension> plus(p: Point<D>) = Point<D>(data.zip(p.data) { x, y -> x + y } )
}
basically the solution was just to make that function a member function, and use the private constructor