https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/03/2024, 6:51 PM
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

Youssef Shoaib [MOD]

03/03/2024, 6:56 PM
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

Adam S

03/03/2024, 6:56 PM
It will compile if you create a suitable constructor for SubA that accepts a single
Any
arg https://pl.kotl.in/uRz_XPAzo
r

Ray Rahke

03/03/2024, 7:20 PM
@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

Youssef Shoaib [MOD]

03/03/2024, 7:22 PM
KFunction<InstanceType>
will do it I believe
r

Ray Rahke

03/03/2024, 7:22 PM
It does, thanks guys
y

Youssef Shoaib [MOD]

03/03/2024, 7:22 PM
Or
KCallable<InstanceType>
even, which generalises to properties too