I'd like some feedbacks, because I have the feelin...
# announcements
e
I'd like some feedbacks, because I have the feeling I'm gonna get bitten by this issue sooner or later. I have generic interfaces
Vec3t<Number>
implemented as
Float
,
Int
, etc.. Of course methods differing only for a different type (i.e:
dot(v: Vec3t<Number>)
and
dot(v: Vec3t<Float>)
) have the same signature, but if I try to call:
Copy code
val vert = Vec3() // Vert3 : Vec3t<Float>
vert.dot(fromTo) // error, none of the following functions can be called. Note that dot is dot(v: Vec3t<Number>)
I have to either change
dot
to
dot(v: Vec3t<Float>)
or create a new set of interfaces, where I have
Vec3tNumber
and
Vec3tFloat
and so on.. What do you think?
a
can you show us the code of your
interface Vec3t
?
a
you've defined the generic type
T
but you are actually using
Number
everywhere. Is there a specific reason for that?
e
I need a generic type that shall be extended by the given implementation
what do you suggest?
a
i have the gut feeling that the whole Vec3 code can be simplified a lot if we make proper use of generics
should the API stay the same?
e
I like your gut feeling
what do you suggest
a
alright, lets start with the smaller ones: in every subclass you have operators like
plus
,
minus
etc
those functions just need a way to create a new instance of the subclass, then you can move many of the implementations into the base-class
Copy code
abstract class Vec3t<T: Number>(...) {
    abstract protected inline fun createInstance(): Vec3t<T>

    operator fun plus(t: T) = plus(createInstance(), this, t, t, t)
    operator fun plus(t: Vec3t<T>) = plus(createInstance(), this, t.x, t.y, t.z)
}
all you have to do now is implement
createInstance
inside your
Vec3i
and you get plus operator for free
same for minus, times, div
e
clever hints
thanks dear
"inline" modifier is not allowed on virtual members
a
eh, remove that
inline
from
createInstance
, its not needed