I’m currently dealing with some generic / nullabil...
# ios
t
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
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
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
🎉
k
Glad it worked out? Sorry, super busy couple weeks 🙂
r
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