Patterns

Wed, 14 Mar 2007 17:36:18 -0700
draft

A pattern can matched against a value. If it matches, one or more variables may be bound to some part of the matched value.
Patterns can be used in various declaration contexts, include variable declaration, parameter declarations, and cases of a switch expression.
Abstract pattern grammar

Here is a classifications of patterns why should support. The concrete syntax is not fixed.
Variables

The simplest pattern is a variable. This declares that variable, and it is bound to the value being matched against. Question: It may make sense to use a special syntactic marker to indicate a variable being declared, as opposed to being used.
Type specification

pattern!type

Conjunction

pattern1&pattern2...

This matches pattern1 against the target, possibly binding some variables. Then pattern2 is matches against the same target. The pattern2 may contain use variables bound in pattern1. Commonly, pattern2 will be a predicate or a type-specifier. In fact, perhaps having a special syntax for conjunction may not be useful, since it can be expressed using a predicate.
Predicate

{boolean-expression}

This matches if the boolean-expression evaluates to true. Typically, boolean-expression may contain variables declared previously in a conjunction. In fact, we could combine the syntaxes:

{pattern|boolean-expression}

Constructor

constructor-name(pattern1, pattern2, ...)