| ||
Tutorial: Virtual Patterns |
To get started, think of virtual patterns as virtual methods. Then continue to discover what else they can do. Virtual patterns used as virtual methodsWhen using virtual patterns just like virtual methods in other statically typed object-oriented languages, there are no new surprises, remembering that specialization does not discard anything, not even behavior. Here's an example: Example 5This is just the previous example, adjusted to a more reasonable shape by using virtual "methods:"
Note that the first (most general) occurrence of a virtual
pattern declaration is marked with
Intermediate declarations, virtual further-bindings, are
marked with Virtual patterns used for genericitySince a pattern is a general descriptor of substance, virtual patterns can do more than "virtual methods." If we use the instantiated substance, i.e. the objects obtained from a given virtual pattern, as "objects", then the virtual pattern works as a type parameter on the enclosing pattern. Example 6
This example shows a data-structure whose contained elements are
qualified by a virtual pattern,
Note that gbeta actually prints two warnings about a potential run-time type errors for this program. Actually, it is statically type-safe, and the type-checker will be able to detect this in a future version of gbeta. Se the section about bugs and inconveniences for more details. Virtual patterns may be viewed as constant, automatically initialized pattern references: a pattern which depends on the actual enclosing current object, even though it is the same for all instances of the same pattern. The next section presents the even more dynamic pattern references. |