Define a primitive data type: Difference between revisions

added Factor
(added Factor)
Line 544:
return i >= 1 and i <= 10
end type</lang>
 
=={{header|Factor}}==
We can accomplish this using a predicate class. A predicate class must be a subclass of an existing class and allows the programmer to write a predicate which determines membership.
<lang factor>PREDICATE: my-int < integer [ 0 > ] [ 11 < ] bi and ;</lang>
This automatically creates the <code>my-int?</code> predicate word which determines whether an object is a <code>my-int</code>.
<lang factor>11 my-int? ! f
10 my-int? ! t
"hello" my-int? ! f</lang>
We can now write methods that specialize on <code>my-int</code>. We will define an increment word, <code>++</code>, which increments <code>integer</code>s by <tt>1</tt> but divides <code>my-int</code>s by <tt>2</tt>.
<lang factor>GENERIC: ++ ( m -- n )
M: integer ++ 1 + ;
M: my-int ++ 2/ ;
 
10 ++ ! 5
11 ++ ! 12</lang>
 
=={{header|Forth}}==
1,827

edits