Constrained genericity: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Fix broken example.) |
m (J: remove stale intro quip, simplify, and add some narrative description) |
||
Line 576: | Line 576: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation: |
|||
J is not a statically typed language, |
|||
but I do not see why we should not implement this in J: |
|||
<lang j>coclass'Connoisseur' |
<lang j>coclass'Connoisseur' |
||
isEdible=:3 :0 |
isEdible=:3 :0 |
||
Line 585: | Line 584: | ||
coclass'FoodBox' |
coclass'FoodBox' |
||
create=:3 :0 |
create=:3 :0 |
||
assert isEdible_Connoisseur_ type=:y |
|||
collection=: 0#y |
collection=: 0#y |
||
) |
) |
||
add=:3 :0"0 |
add=:3 :0"0 |
||
'inedible' assert |
'inedible' assert isEdible_Connoisseur_ y |
||
collection=: collection, y |
collection=: collection, y |
||
EMPTY |
|||
)</lang> |
)</lang> |
||
An edible type would be a class that has a verb with the name 'eat' |
An edible type would be a class that has a verb with the name 'eat' (the task "eatable" requirement is checked on an object or class reference using the static method <code>isEdible_Connoisseur_</code>). |
||
We have also defined a 'FoodBox' container class which can only contain edible objects. (Our add method returns returns an empty result since its purpose is to add to the container, not to produce a result.) |
|||
For example: |
|||
<lang j>coclass'Apple' |
<lang j>coclass'Apple' |
||
eat=:3 :0 |
eat=:3 :0 |
||
Line 599: | Line 602: | ||
And here is a quicky demo of the above: |
And here is a quicky demo of the above: |
||
<lang j> |
<lang j> |
||
lunch=: |
lunch=:'' conew 'FoodBox' |
||
a1=: conew 'Apple' |
a1=: conew 'Apple' |
||
a2=: conew 'Apple' |
a2=: conew 'Apple' |