russhwolf
06/04/2018, 12:44 AMMyObject.Companion.foo()
in kotlin code compiling to a framework, what’s the proper way to access it from Swift? The only thing I can figure out how to do is MyObjectCompanion.init().foo()
, but that doesn’t seem right because I don’t want to be creating a new instance of the companion object.svyatoslav.scherbina
06/04/2018, 9:44 AMMyObjectCompanion().foo()
is right. It actually doesn’t create a new instance, but returns the single instance every time.
I believe this way to expose Kotlin object
declaration follows Swift and Objective-C conventions about singletons, isn’t it?russhwolf
06/04/2018, 12:03 PMKonstantin Petrukhnov
06/04/2018, 12:51 PMKonstantin Petrukhnov
06/04/2018, 12:52 PMsvyatoslav.scherbina
06/04/2018, 1:13 PMrusshwolf
06/04/2018, 1:24 PMMyObjectCompanion().foo()
is being called from Swift, not Kotlin. On JVM in Kotlin it would look like MyObject.foo()
the same as always.drofwarcs
06/04/2018, 1:26 PMMyObject.foo()
. I understand why K/N needs to create two separate classes, but do not understand why the foo function is created as an instance method under MyObjectCompanion. I believe the solution proposed here https://github.com/JetBrains/kotlin-native/issues/1549 whould be a good secondary option to expose similar syntax.drofwarcs
06/04/2018, 1:29 PMMyObject
instance methods can be called normally but if you need to call its static methods you need to call a different class, i.e. MyObjectCompanion
russhwolf
06/04/2018, 1:33 PMyusuf3000
06/04/2018, 1:54 PMsvyatoslav.scherbina
06/05/2018, 8:17 AMobject
members, but it must be done carefully.
the solution proposed here https://github.com/JetBrains/kotlin-native/issues/1549 whould be a good secondary option to expose similar syntax.Which solution do you mean?
yusuf3000
06/05/2018, 11:14 AMyusuf3000
06/05/2018, 11:18 AMclass Test
in kotlin with 1 instance method method1()
and a companion object with an init. I want to call the init method from Swift so I call TestCompanion()
, but then I can not invoke method1
on this instance as this init returns TestCompanion
rather than Test
svyatoslav.scherbina
06/05/2018, 12:39 PMinit
method declared in Kotlin companion object?
If so, then it must be available as TestCompanion().doInit()
in Swift.drofwarcs
06/05/2018, 2:26 PM@ObjCStatic
option. The reasoning for having this option is the exact same reason I believe @JVMStatic
was created on the JVM side. Somewhere along the development of the Kotlin language, it was deemed useful to allow Java apps to call a Kotlin companion object as if it was a static method instead of always doing MyObject.Companion.foo()
. I feel as more Kotlin iOS frameworks are created, the community may see a similar need for that platform.drofwarcs
06/05/2018, 2:31 PMdrofwarcs
06/05/2018, 2:36 PM@JVMStatic
svyatoslav.scherbina
06/06/2018, 7:18 AM@JvmStatic
is the solution that we are considering.