-
Notifications
You must be signed in to change notification settings - Fork 564
Improve support for -Xobjc-generics and enable it by default #3778
Conversation
OBJC_INTEROP.md
Outdated
@@ -229,26 +229,26 @@ foo { | |||
Objective-C supports "lightweight generics" defined on classes, with a relatively limited feature set. Swift can import | |||
generics defined on classes to help provide additional type information to the compiler. | |||
|
|||
Generic feature support for Objc and Swift differ from Kotlin, so the translation will inevitably lose some information, | |||
Generic feature support for Obj-C and Swift differ from Kotlin, so the translation will inevitably lose some information, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Objective-C
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
|
||
fun isTypeParameterNameReserved(name: String): Boolean = name in reservedTypeParameterNames | ||
|
||
private val reservedTypeParameterNames = setOf("id", "NSObject", "NSArray", "NSCopying", "NSNumber", "NSInteger", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where this list comes from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally from here: https://github.com/JetBrains/kotlin-native/pull/3778/files#diff-d13ae8cff625fe7530e3a86b4102ca0fL666
I have some plans to improve this part of code, but it requires more deep changes.
@@ -216,10 +216,10 @@ class StdlibTests : TestProvider { | |||
try applyVoid { $0.add("bar") } | |||
try applyVoid { $0.remove("baz") } | |||
try applyVoid { $0.add("baz") } | |||
try applyVoid { $0.add(TripleVals(first: 1, second: 2, third: 3)) } | |||
try apply { $0.member(TripleVals(first: 1, second: 2, third: 3)) as! TripleVals } | |||
try applyVoid { $0.add(TripleVals<NSNumber>(first: 1, second: 2, third: 3)) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why it is needed to specify NSNumber
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift compiler infers Int
as type argument, but Objective-C classes can have only "class" (e.g. reference) types as type arguments.
elements.set(index: 0, value: nil) | ||
elements.set(index: 1, value: 42) | ||
elements.set(index: 1, value: 42 as NSNumber) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it always required now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly. Swift doesn't actually have implicit conversion from its integer types to NSNumber
. There is ad hoc support for literals, e.g.
let x: NSNumber = 42
works.
But in the discussed line of test the expected type is not NSNumber
, so such a literal conversion doesn't apply.
OBJC_INTEROP.md
Outdated
@@ -327,6 +312,20 @@ In Kotlin you can provide upper bounds for a generic type. Objective-C also supp | |||
in more complex cases, and is currently not supported in the Kotlin - Objective-C interop. The exception here being a non-null | |||
upper bound will make Objective-C methods/properties non-null. | |||
|
|||
### To disable | |||
|
|||
To have the framework header written without generics, add a flag to the compiler config: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the flag
? It's precisely that flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Btw, this sense was originally written by a native speaker ¯\_(ツ)_/¯
as it seems to be referenced from code generated by Swift compiler under certain circumstances with -Xobjc-generics involved. Also #KT-31665 Fixed
* Avoid emitting accidentally nested protocols * Unify PSI and descriptors implementation (thus fix it with generics involved in ObjCExportLazy) * Avoid class name translation dependency on its contents * Simplify the policy
current heuristic is wierd with generics enabled and less important in this case.
7876cee
to
930f4ce
Compare
No description provided.