Files
swift-mirror/test/Generics/function_defs.swift
Doug Gregor f847fe4a22 Introduce basic support for type-checking the definitions of generic
functions. This involves a few steps:

  - When assigning archetypes to type parameters, also walk all of the
  protocols to which the type parameter conforms and assign archetypes
  to each of the associated types.
  - When performing name lookup into an archetype, look into all of
  the protocols to which it conforms. If we find something, it can be
  referenced via the new ArchetypeMemberRefExpr.
  - When type-checking ArchetypeMemberRefExpr, substitute the values
  of the various associated types into the type of the member, so the
  resulting expression involves the archetypes for the enclosing
  generic method.

The rest of the type checking essentially follows from the fact that
archetypes are unique types which (therefore) have no behavior beyond
what is provided via the protocols they conform to. However, there is
still much work to do to ensure that we get the archetypes set up
correctly.



Swift SVN r2201
2012-06-19 21:16:14 +00:00

27 lines
748 B
Swift

// RUN: %swift %s -verify
//===----------------------------------------------------------------------===//
// Type-check function definitions
//===----------------------------------------------------------------------===//
protocol EqualComparable {
func isEqual(other : This) -> Bool
}
func doCompare<T : EqualComparable, U : EqualComparable>(t1 : T, t2 : T, u : U) -> Bool {
var b1 = t1.isEqual(t2)
if (b1) {
return true;
}
t1 = t2
return t1.isEqual(u) // expected-error{{invalid conversion from type 'U' to 'T'}} // expected-note{{while converting}}
}
protocol MethodLessComparable {
func isLess(other : This) -> Bool
}
func min<T : MethodLessComparable>(x : T, y : T) -> T {
if (y.isLess(x)) { return y; }
return x;
}