Files
swift-mirror/test/Constraints/casts.swift
Joe Groff 9667bda089 Implement 'as' syntax for coercions and casts.
Provide distinct syntax 'a as T' for coercions and 'a as! T' for unchecked downcasts, and add type-checker logic specialized to coercions and downcasts for these expressions. Change the AST representation of ExplicitCastExpr to keep the destination type as a TypeLoc rather than a subexpression, and change the names of the nodes to UncheckedDowncast and UncheckedSuperToArchetype to make their unchecked-ness explicit and disambiguate them from future checked casts.

In order to keep the changes staged, this doesn't yet affect the T(x) constructor syntax, which will for the time being still perform any construction, coercion, or cast.

Swift SVN r4498
2013-03-27 22:27:11 +00:00

21 lines
408 B
Swift

// RUN: %swift -parse -verify %s
class B { }
class D : B { }
var seven = 7 as Double
var pair = (1, 2) as (Int, Double)
var closure = { $0 + $1 } as (Int, Int) -> Int
var d_as_b = new D as B
var b_as_d = new B as! D
var bad_b_as_d = new B as D // expected-error{{}}
func archetype_casts<T:B>(t:T) {
var t_as_b = t as B
var b_as_t = new B as! T
var bad_b_as_t = new B as T // expected-error{{}}
}