1 ! (c)2009 Joe Groff bsd license
2 USING: arrays classes classes.singleton classes.tuple help.markup
3 help.syntax kernel multiline slots quotations ;
11 tuple: { slot slot slot ... }
16 { $description "Defines " { $snippet "class-name" } " as a union of the following " { $link singleton-class } " and " { $link tuple-class } " definitions. Each " { $snippet "singleton" } " word is defined as a " { $snippet "singleton-class" } ", and each " { $snippet "tuple" } " word is defined aas a " { $snippet "tuple-class" } " with the given set of " { $snippet "slot" } "s, using the same syntax for slot specifiers as " { $link POSTPONE: TUPLE: } ". Typed tuple slots can recursively reference the variant " { $snippet "class-name" } " being defined. For " { $snippet "tuple" } " types, a " { $link boa } " constructor word " { $snippet "<tuple>" } " is defined as well." }
17 { $examples { $code """
18 USING: kernel variants ;
23 cons: { { first object } { rest list } }
28 { $values { "branches" array } }
29 { $description "Dispatches on the type of the value on the top of the stack. If the type is a " { $link singleton-class } ", the corresponding quotation is called with the underlying stack unchanged. If the type is a " { $link tuple-class } ", the tuple slots are pushed onto the stack by order of arguments." }
30 { $examples { $example """
31 USING: kernel math prettyprint variants ;
36 cons: { { first object } { rest list } }
39 : list-length ( list -- length )
42 { cons [ nip list-length 1 + ] }
45 1 2 3 4 nil <cons> <cons> <cons> <cons> list-length .
49 { $values { "class" class } }
50 { $description "Decomposes a tuple of type " { $snippet "class" } " into its component slot values by order of arguments. The inverse of " { $link boa } "." } ;
53 { $class-description "This class comprises class names that have been defined with " { $link POSTPONE: VARIANT: } ". When a " { $snippet "variant-class" } " is used as the type of a specialized " { $link tuple } " slot, the variant's first member type is used as the default " { $link initial-value } "." } ;
55 { POSTPONE: VARIANT: variant-class match } related-words
57 ARTICLE: "variants" "Algebraic data types"
58 "The " { $vocab-link "variants" } " vocabulary provides syntax and combinators for defining and manipulating algebraic data types."
59 { $subsection POSTPONE: VARIANT: }
60 { $subsection variant-class }
61 { $subsection match } ;