Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.24 KB

Bounded generics.md

File metadata and controls

50 lines (36 loc) · 1.24 KB

Bounded generics

The generic type restriction is not supported.

Explanations

Let's describe a generic class limited to a specific class in Kotlin:

open class ForStricted  
  
class ChildStricted : ForStricted()
  
class StrictedGeneric<T : ForStricted>(val data: T) {  
    fun fetch(): T {  
        return data  
    }  
}  
  
private fun example() {  
    val s1 = StrictedGeneric(ForStricted())  
    val s2 = StrictedGeneric(ChildStricted())  
	
    // val s3 = StrictedGeneric("123") // Doesn't compile
}

There is no information about the generic limitation in the Objective-C file:

__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("StrictedGeneric")))
@interface SharedStrictedGeneric<T> : SharedBase
- (instancetype)initWithData:(T)data __attribute__((swift_name("init(data:)"))) __attribute__((objc_designated_initializer));
- (T)fetch __attribute__((swift_name("fetch()")));
@property (readonly) T data __attribute__((swift_name("data")));
@end

Therefore, in Swift we can compile the following code:

class MyStricted : ForStricted {}

let _ = StrictedGeneric(data: MyStricted())
let _ = StrictedGeneric(data: NSString("1122")) // Compiles

Table of contents