Is there a way to pass a generic parameter to a (s...
# announcements
s
Is there a way to pass a generic parameter to a (secondary) constructor? If it was any other method, I would do
fun <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
)
s
Create a so-called smart constructor:
Copy code
class MyClass private constructor(....) {
 
    companion object {
        operator fun <T> invoke(param:T , ...): MyClass = ...

        operator fun invoke(...): MyClass = ...
    }
}
🤩 2
💯 4
n
damn
that's clever
shouldn't be necessary. but still clever.
s
Smart-constructors also ‘allow’ you to validate the input and decide to return
null
or throw an exception or something similar….
s
ooo that IS clever
s
Or,
MyClass
can be an abstract class and the
operator fun invoke
calls will return sub-classes/implementations
n
what I really like about this, more generally
is that you can do work before calling the primary constructor
It's kind of annoying and seemingly pointlessly asymmetric that you can do work after, but not really before, the primary constructor call, in a secondary constructor
e
other hacks to do work in secondary constructors before calling primary/super:
Copy code
class Foo(x, y, z) {
    constructor(input) : this(computation(input))
    private constructor(result) : this(computed.x, computed.y, computed.z)
}
s
Interesting that’s actually possible in Java (
public <T> MyClass(T param)
) but not in kotlin without overriding
invoke
n
@ephemient yeah i've used these hacks extensively in C++ 🙂 was hoping to never need to use them again