5.8. As Pattern

The as keyword binds a variable to the value matched by a pattern. It is a more general way of binding variables than using variable patterns alone.

5.8.1. Syntax

[44]AsPattern::= Pattern as Variable  

5.8.2. Type

When P as x is matched against a type T, the pattern P is matched against T, producing typing context Γ. The typing context produced by the whole match is Γ ∪ {x has type T}.

5.8.3. Examples

Simplified Fragment
{- Consider this initial program fragment, without an 'as' pattern -}
val (a,b) = ((1,2),(3,4))
val (ax,ay) = a
val (bx,by) = b

{- Compared to the following fragment -}
val ((ax,ay) as a, (bx,by) as b) = ((1,2),(3,4))

[ax, ay, a] | [bx, by, b]

[1, 2, (1, 2)]
[3, 4, (3, 4)]

5.8.4. Related Links

Related Reference Topics

Related Tutorial Sections