Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Commit

Permalink
Improve -Xobjc-generics documentation formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SvyatoslavScherbina committed Jan 27, 2020
1 parent 1f397ab commit 8259e74
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions OBJC_INTEROP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 Objective-C and Swift differ from Kotlin, so the translation will inevitably lose some information,
but the features supported retain meaningful information.

#### Limitations

Objective-C generics do not support all features of either Kotlin or Swift, so there will be some information lost
in the translation.

Generics can only be defined on classes, not on interfaces (protocols in Objc and Swift) or functions.
Generics can only be defined on classes, not on interfaces (protocols in Objective-C and Swift) or functions.

#### Nullability

Kotlin and Swift both define nullability as part of the type specification, while Objc defines nullability on methods
Kotlin and Swift both define nullability as part of the type specification, while Objective-C defines nullability on methods
and properties of a type. As such, the following:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
class Sample<T>(){
fun myVal():T
class Sample<T>() {
fun myVal(): T
}
```

Expand All @@ -259,29 +259,29 @@ will (logically) look like this:
<div class="sample" markdown="1" theme="idea" mode="swift">

```swift
class Sample<T>(){
fun myVal():T?
class Sample<T>() {
fun myVal(): T?
}
```

</div>

In order to support a potentially nullable type, the Objc header needs to define `myVal` with a nullable return value.
In order to support a potentially nullable type, the Objective-C header needs to define `myVal` with a nullable return value.

To mitigate this, when defining your generic classes, if the generic type should *never* be null, provide a non-null
type constraint:

<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
class Sample<T:Any>(){
fun myVal():T
class Sample<T : Any>() {
fun myVal(): T
}
```

</div>

That will force the Objc header to mark `myVal` as non-null.
That will force the Objective-C header to mark `myVal` as non-null.

#### Variance

Expand All @@ -291,8 +291,8 @@ from Objective-C can be force-cast as needed.
<div class="sample" markdown="1" theme="idea" data-highlight-only>

```kotlin
data class SomeData(val num:Int = 42):BaseData()
class GenVarOut<out T:Any>(val arg:T)
data class SomeData(val num: Int = 42) : BaseData()
class GenVarOut<out T : Any>(val arg: T)
```

</div>
Expand Down

0 comments on commit 8259e74

Please sign in to comment.