Constrained genericity: Difference between revisions
Content added Content deleted
(→{{header|Scala}}: ++ sather, hopefully I've taken it right) |
(Added PicoLisp) |
||
Line 316: | Line 316: | ||
let your_box = FloatBox.make_box_from_list [2.3; 4.5]</lang> |
let your_box = FloatBox.make_box_from_list [2.3; 4.5]</lang> |
||
Unfortunately, it is kind of cumbersome in that, for every type parameter we want to use for this generic type, we will have to explicitly create a module for the resulting type (i.e. <tt>BananaBox</tt>, <tt>FloatBox</tt>). And the operations on that resulting type (i.e. <tt>make_box_from_list</tt>) are tied to each specific module. |
Unfortunately, it is kind of cumbersome in that, for every type parameter we want to use for this generic type, we will have to explicitly create a module for the resulting type (i.e. <tt>BananaBox</tt>, <tt>FloatBox</tt>). And the operations on that resulting type (i.e. <tt>make_box_from_list</tt>) are tied to each specific module. |
||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(class +Eatable) |
|||
(dm eat> () |
|||
(prinl "I'm eatable") ) |
|||
(class +FoodBox) |
|||
# obj |
|||
(dm set> (Obj) |
|||
(unless (method 'eat> Obj) # Check if the object is eatable |
|||
(quit "Object is not eatable" Obj) ) |
|||
(=: obj Obj) ) # If so, set the object |
|||
(let (Box (new '(+FoodBox)) Eat (new '(+Eatable)) NoEat (new '(+Bla))) |
|||
(set> Box Eat) # Works |
|||
(set> Box NoEat) ) # Gives an error</lang> |
|||
Output: |
|||
<pre>$384320489 -- Object is not eatable |
|||
? (show Box) |
|||
$384320487 (+FoodBox) |
|||
obj $384320488 |
|||
? (show Box 'obj) |
|||
$384320488 (+Eatable) |
|||
? (show NoEat) |
|||
$384320489 (+Bla)</pre> |
|||
=={{header|Sather}}== |
=={{header|Sather}}== |