I have some base class which is wrapper over BitSet
. It has multiple descendants. In base class I want to force descendants to implement validate
method for constructor which accepts long. It will be different for each child. So I’ve created the following base class:
abstract class BaseClassModel {
private val bitset: BitSet
private val maxBytes = 64
constructor(longValue: Long) {
validate(longValue)
bitset = BitSet.valueOf(longArrayOf(longValue))
}
constructor(vararg attributes: BaseAttribute) {
bitset = BitSet(maxBytes)
attributes.forEach { set(it) }
}
abstract fun validate(longValue: Long)
...
}
interface BaseAttribute{
val bitIndex: Int
}
But at this case I receive the warning:
Calling non-final function validate in constructor
I thoroghly read Kotlin calling non final function in constructor works and why this warning is shown but I don’t know how to avoid it. Moreover I’m sure that other fields ofBaseClassModel
won’t be used in validation method.
I see the options:
- Call
validate
in constructor of each child. At this case I see risks that someone could forget to call validate. - Ignore this warning. It is just warning.
Is there better option ?