Trying to annotate a variable's type as any class ...
# getting-started
r
Trying to annotate a variable's type as any class constructor that produces an instance who's type is a subclass of some base class. Don't understand why it is failing. I am trying to use
Any
to mean "The arguments of the class constructor are not relevant. Any constructor works independent of parameters, we just care about if it creates an object of type `InstanceType`"
y
Firstly, using
Any
in this situation will never work because a function that takes
Any
is more specific than a function that takes
Char
. What you actually would want is a function that takes
Nothing
, which also means that you can't call it. Secondly, you're using
KFunction1
, so it'll only work for functions of 1 argument. What you actually want is
KFunction
, which doesn't have a specified arity
a
It will compile if you create a suitable constructor for SubA that accepts a single
Any
arg https://pl.kotl.in/uRz_XPAzo
r
@Adam S It should be independent of the constructor signature of the subclass
I just want to express "some constructor whose created object is of this specified type"
does KFunction do it?
let me try
y
KFunction<InstanceType>
will do it I believe
r
It does, thanks guys
y
Or
KCallable<InstanceType>
even, which generalises to properties too