// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) package stdgen // **** demo classes **** open class P { fun mP() { } } open class C : P() { fun mC() { } } class Container { var o: T? = null fun put(o: T?) { this.o = o } fun get(): T? { return o } } // **** standard generic methods **** fun test(co: Container, o: T) { //co.put(P()) // no required relation between T and P //co.put(C()) // no required relation between T and P co.put(o) //val pcopy: P? = co.get() // no required relation between T and P //pcopy?.mP() //pcopy?.mC() //val ccopy: C? = co.get() // no required relation between T and P //ccopy?.mP() //ccopy?.mC() val tcopy: T? = co.get() //tcopy?.mP() //tcopy?.mC() } fun testp(co: Container, o: T) { //co.put(P()) // problem if T is C or a subclass of C //co.put(C()) // problem if T is a subclass of C co.put(o) val pcopy: P? = co.get() pcopy?.mP() //pcopy?.mC() //val ccopy: C? = co.get() // problem if T is P //ccopy?.mP() //ccopy?.mC() val tcopy: T? = co.get() tcopy?.mP() //tcopy?.mC() } fun testc(co: Container, o: T) { //co.put(P()) // problem if T is C or a subclass of C //co.put(C()) // problem if T is a subclass of C co.put(o) val pcopy: P? = co.get() pcopy?.mP() //pcopy?.mC() val ccopy: C? = co.get() ccopy?.mP() ccopy?.mC() val tcopy: T? = co.get() tcopy?.mP() tcopy?.mC() } // **** fun main() { test(Container

(), P()) test(Container(), C()) testp(Container

(), P()) testp(Container(), C()) //testc(Container

(), P()) // P does not match "T extends C" testc(Container(), C()) }