https://kotlinlang.org logo
#ios
Title
# ios
t

Tijl

03/29/2021, 6:29 PM
I’m currently dealing with some generic / nullability issues with ObjC/Swift/Kotlin interop. Below are three different ways of defining the generic type for a method and the generated header (irrelevant stuff stripped off)
Copy code
class Foo<T> { // no upper bound 
    fun bar(baz: (T) -> Unit) {
    }
}

@interface KNFFoo<T> : KNFBase // leads to T _Nullable as expected
- (void)barBaz:(void (^)(T _Nullable))baz __attribute__((swift_name("bar(baz:)")));
@end;

class Foo<T:Any> { // Upper bound of Any
    fun bar(baz: (T) -> Unit) {
    }
}

@interface KNFFoo<T> : KNFBase
- (void)barBaz:(void (^)(T))baz  // leads to T as expected __attribute__((swift_name("bar(baz:)")));
@end;

class Foo<T> {
    fun bar(baz: (T:Any) -> Unit) { // upperbound of Any just for the method
    }
}

@interface KNFFoo<T> : KNFBase
- (void)barBaz:(void (^)(id))baz // <----- id not T? __attribute__((swift_name("bar(baz:)")));
Anyone a little more familiar than me know if my assumption this could be T instead of id is correct?
@kpgalligan I believe you originally contributed the code for objc generics, hope you don’t mind me tagging you
b

basher

03/30/2021, 11:47 PM
Hmm is that last one actually adding a constraint in Kotlin? Looks like it might actually be a param called "capital T"
I thought constraints have to go in angle brackets near the "fun" declaration
t

Tijl

03/31/2021, 5:28 AM
you must be right, I’ll check to make sure
indeed, thank you @basher. I didn’t even know lambda’s could have named arguments, and I was already confused why this worked in the first place. But this was due to an
is
I had somewhere smart-casting the Any I was passing in my non-simplified code.
fun <T:Any>baz(t:T)
is accepted so I’ll test if that works
ah, that just makes it Any in that scope
b

basher

03/31/2021, 3:25 PM
🎉
k

kpgalligan

03/31/2021, 3:26 PM
Glad it worked out? Sorry, super busy couple weeks 🙂
r

russhwolf

03/31/2021, 8:50 PM
I'm not clear that it did work out? If you want it to be
T
instead of
Any
from Swift, you need to do
class Foo<T: Any>
and
fun baz(t: T)
. Only generic declarations on classes (rather than interfaces or functions) get exposed to Obj-C
2 Views