StavFX
08/11/2020, 6:07 PMfun <T> foo(param: T)
, but with a constructor I can’t just do <T> constructor(param: T)
There are ways I can get around this limitation, but I’m just curious if it’s possible with slightly different syntax.
(also, I don’t want to make the entire class generic to T
)streetsofboston
08/11/2020, 6:13 PMclass MyClass private constructor(....) {
companion object {
operator fun <T> invoke(param:T , ...): MyClass = ...
operator fun invoke(...): MyClass = ...
}
}
Nir
08/11/2020, 6:15 PMNir
08/11/2020, 6:15 PMNir
08/11/2020, 6:15 PMstreetsofboston
08/11/2020, 6:16 PMnull
or throw an exception or something similar….StavFX
08/11/2020, 6:16 PMstreetsofboston
08/11/2020, 6:17 PMMyClass
can be an abstract class and the operator fun invoke
calls will return sub-classes/implementationsNir
08/11/2020, 6:20 PMNir
08/11/2020, 6:20 PMNir
08/11/2020, 6:21 PMephemient
08/11/2020, 6:26 PMclass Foo(x, y, z) {
constructor(input) : this(computation(input))
private constructor(result) : this(computed.x, computed.y, computed.z)
}
StavFX
08/11/2020, 6:27 PMpublic <T> MyClass(T param)
) but not in kotlin without overriding invoke
Nir
08/11/2020, 6:28 PM