How to get actual type arguments of a reified generic parameter in Kotlin?

Due to type erasure, actual generic arguments cannot be obtained through T::class token of a generic class. Different objects of a class must have the same class token, that’s why it cannot contain actual generic arguments. Edit: Since Kotlin 1.3.50, following the technique described below to get type information for a reified type parameter is … Read more

Swift equivalent for MIN and MAX macros

min and max are defined in Swift: func max<T : Comparable>(x: T, y: T, rest: T…) -> T func min<T : Comparable>(x: T, y: T, rest: T…) -> T and used like so: let min = min(1, 2) let max = max(1, 2) See this great writeup on documented & undocumented built-in functions in Swift.

How to get generic parameter class in Kotlin

For a class with generic parameter T, you cannot do this because you have no type information for T since the JVM erases the type information. Therefore code like this cannot work: class Storage<T: Any> { val snapshot: Snapshot? = … fun retrieveSomething(): T? { return snapshot?.getValue(T::class.java) // ERROR “only classes can be used…” } … Read more

Generic NSOperation subclass loses NSOperation functionality

Workaround: You can create NSOperation subclass (no generic), override main and call you own ‘execute’ func, which can be overriden by generic subclasses. Example: class SwiftOperation : NSOperation { final override func main() { execute() } func execute() { } } class MyOperation<T> : SwiftOperation { override func execute() { println(“My operation main was called”) … Read more