https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
k

kgonyon

01/21/2019, 9:58 PM
Is there a way to grab a
KClass
of an object defined in Kotlin from Swift? I have a function defined in Kotlin that has an argument
type: KClass
and I need a way to call that function from Swift, but not sure how to pass in the type.
s

svyatoslav.scherbina

01/22/2019, 8:19 AM
Do I understand correctly that you need to get a
KClass
of an instance? Like in
fun getClassOf(obj: Any) = obj::class
?
k

kgonyon

01/22/2019, 4:13 PM
I believe that is indeed what I needed. I'll let you know if this worked out.
Okay so the problem I am having is I don't have an instance of the type. I am essentially trying to write
get(type: Foo::class)
in Swift. The kotlin function definition would be
fun <T> get(type: KClass<T>): T
. In the context which I am using it, I have a global factory to inject instances of a presenter thus I don't have the instance just the class that I want an instance of.
s

svyatoslav.scherbina

01/23/2019, 7:57 AM
Is
Foo
a class or an interface?
k

kgonyon

01/23/2019, 3:02 PM
Foo
would be an abstract class defined in Kotlin.
s

svyatoslav.scherbina

01/24/2019, 12:51 PM
We consider implementing this feature later.
k

kgonyon

01/24/2019, 4:22 PM
I can get around this issue by creating a factory method in the companion object of the abstract class instead of creating a centralized factory object.
2 Views