Hylke Bron
11/01/2022, 9:53 AM@Throws(Throwable::class) public suspend fun getSomething(): String
because in my experience returning sealed classes with Success / Failure types to swift (via objective c) does not work nicely with swift.
This approach works OK with swift since the exceptions are checked, but when using this same method in android kotlin, there is no checked exceptions so its easy to forget to add a try catch around it.
So I was wondering what your approach is.ayodele
11/01/2022, 11:10 AMHylke Bron
11/01/2022, 3:16 PMResult<String>
, but id
, that is not really usable from a swift perspectiveHylke Bron
11/01/2022, 3:32 PM@Throws(Throwable::class) public suspend fun getSomthingOrThrow(): String
Jeff Lockhart
11/01/2022, 3:53 PMResult
generic type explicitly an NSObject
subclass. With String
, maybe try expect/actual the type to String
in Kotlin and NSString
in ObjC.Hylke Bron
11/01/2022, 3:56 PMResult
class? It is provided by the std and its an @JvmInline value class
, so i think its not possible using the Result
type.Jeff Lockhart
11/01/2022, 4:00 PMResult
type, but for its generic type. Something like:
expect class MyResultType
// Android
actual typealias MyResultType = String
// iOS
actual typealias MyResultType = NSString
...
val result: Result<MyResultType> = ...
Of course you'll want MyResultType
to behave like a String
in common code, so the above code won't be all that's required. But might check if it will resolve the generic type in ObjC correctly first though.Hylke Bron
11/01/2022, 4:04 PMResult
type itself is erased because its an inline value class?
The Result<T>
type of the kotlin std is unusable because inline classes are unsupported. (Result<T> is defined like so:
@JvmInline
public value class Result<out T> @PublishedApi internal constructor(
Jeff Lockhart
11/01/2022, 4:06 PMNSObject
subclass.Hylke Bron
11/01/2022, 4:09 PMpublic fun test(): Result<NSString> { TODO() }
translates to
+ (id _Nullable)test __attribute__((swift_name("test()")));
Jeff Lockhart
11/01/2022, 4:09 PMJeff Lockhart
11/01/2022, 4:11 PMResult
is @JvmInline
. So the native version shouldn't be inlined, right?Hylke Bron
11/01/2022, 4:12 PMHylke Bron
11/01/2022, 4:12 PMJeff Lockhart
11/01/2022, 4:13 PMvalue class
. You might try creating your own Result
type to see if you can get it to work with a non-inlined class. As a last resort, you could even make it non-generic.Hylke Bron
11/01/2022, 4:15 PMJeff Lockhart
11/01/2022, 4:31 PMayodele
11/01/2022, 4:35 PMHylke Bron
11/01/2022, 4:59 PMayodele
11/02/2022, 12:26 PMResult
. Including CancellationExceptions
. So our iOS guys only has to focus on the result type.
It is tricky but it works so far for us. And we agreed on this approach together for both iOS and Android.Hylke Bron
11/02/2022, 12:28 PMpublic fun doSomething(completionBlock: (MyResult<String>) -> Unit)
ayodele
11/02/2022, 12:49 PMHylke Bron
11/02/2022, 12:51 PMayodele
11/02/2022, 12:51 PMpublic fun doSomething(success: (Mydata) -> Unit, error: (Whatever) -> Unit)
Hylke Bron
11/02/2022, 12:53 PMHylke Bron
11/02/2022, 12:53 PMayodele
11/02/2022, 12:53 PMayodele
11/02/2022, 12:55 PM```But you lose the ability to cancel the method (or do you return some handle to cancel the method?) and its not really idiomatic code for the android/kotlin side anymore imo
```Yep you can wrap an adapter around the shared code. It was discussed in the video
ayodele
11/02/2022, 12:55 PMrusshwolf
11/02/2022, 1:00 PMHylke Bron
11/02/2022, 1:27 PMkpgalligan
11/02/2022, 1:54 PMkpgalligan
11/02/2022, 1:54 PMkpgalligan
11/02/2022, 1:55 PMHylke Bron
11/02/2022, 2:33 PMHylke Bron
11/02/2022, 2:35 PM