Sum data type: Difference between revisions

Add Factor example
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
(Add Factor example)
Line 34:
, ( VOID ): print( ( "empty", newline ) )
ESAC</lang>
 
=={{header|Factor}}==
This is accomplished by defining a tuple with only one slot. The slot should have a class declaration that is a union class. This ensures that the slot may only contain objects of classes that are in the union. A convenient way to do this is with an anonymous union, as in the example below. An explicit <code>UNION:</code> declaration may also be used.
 
In the example below, we declare a <code>pseudo-number</code> tuple with one slot that can hold either a <code>number</code> (a built-in class) or a <code>numeric-string</code> — a class which we have declared to be any string that can parse as a number using the <code>string>number</code> word.
<lang factor>USING: accessors kernel math math.parser strings ;
 
PREDICATE: numeric-string < string string>number >boolean ;
 
TUPLE: pseudo-number { value union{ number numeric-string } } ;
 
C: <pseudo-number> pseudo-number ! constructor
 
5.245 <pseudo-number> ! ok
"-17" >>value ! ok
"abc42" >>value ! error</lang>
 
=={{header|Go}}==
1,808

edits