Files
swift-mirror/test/Generics/inheritance.swift
Doug Gregor be915b9c04 Implement semantic analysis for inherits-from constraints.
This change enables inheritance constraints such as "T : NSObject",
which specifies that the type parameter T must inherit (directly or
indirectly) from NSObject. One can then implicit convert from T to
NSObject and perform (checked) downcasts from an NSObject to a T. With
this, we can type-

IR generation still needs to be updated to handle these implicit
conversions and downcasts. New AST nodes may follow.


Swift SVN r3459
2012-12-12 23:28:19 +00:00

44 lines
849 B
Swift

// RUN: %swift %s -verify
class A {
func foo() { }
}
class B : A {
func bar() { }
}
class Other { }
func acceptA(a : A) { }
func f0<T : A>(obj : T, a : A, b : B) { // expected-note{{found this candidate}}
// Method access
obj.foo()
obj.bar() // expected-error{{}}
// Calls
acceptA(obj)
// Derived-to-base conversion for assignment
a = obj
// Invalid assignments
obj = a // expected-error{{}}
obj = b // expected-error{{}}
// Downcasts
a = A(obj)
b = B(obj)
}
func call_f0(a : A, b : B, other : Other) {
f0(a, a, b)
f0(b, a, b)
f0(other, a, b) // expected-error{{}}
}
// Declaration errors
func f1<T : A requires T : Other>() { } // expected-error{{generic parameter 'T' cannot be a subclass of both 'A' and 'Other'}}
func f2<T : A requires T : B>() { } // FIXME: expected-error{{cannot be a subclass}}