inheritancekotlinequalityclass-extensions

data and open incompatibility, which alternative do I have?


I am doing a kotlin port of gli and I got stuck now

Actually I have the following

open class Texture

extended by several classes such as Texture2d

Now, Texture has quite some properties

protected val storage: StorageLinear?
protected val target: Target
protected val format: Format
protected val baseLayer: Int
protected val maxLayer: Int
protected val baseFace: Int
protected val maxFace: Int
protected val baseLevel: Int
protected val maxLevel: Int
protected val swizzles: Swizzles

therefore I'd like to have it as a data class in order to exploit the equals() that comes with it..

but unfortunately data and open are incompatible..(see this question)

One way I can solve it, I could write my own equals() method, but that would be boilerplate code and dirty, exactly two of the biggest reasons why I switched to kotlin over java

Another way, since all the classes extending Texture don't really add anything, they act kind of builders (looks cpp Texture2d class), would it be that of using them as.. builders (see this excellent answer from Kirill)

But since nothing comes for free, of big disadvantage of this solution is that I'd lose the possibility to have Texture2d as a class and Texture2d has a nice operator [] to retrieve single images of textures..

Since the kotlin community looks extremely active and supporting, I'd like to know if you have any better idea..


Solution

  • IMHO the most practical solution would be to generate equals in Texture and be done with it.

    Data classes have a very narrow application on purpose. Your example does not really fit a classical data class, that's why you have this problem.