Constrained genericity: Difference between revisions
m
J: remove stale intro quip, simplify, and add some narrative description
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Fix broken example.) |
m (J: remove stale intro quip, simplify, and add some narrative description) |
||
Line 576:
=={{header|J}}==
Implementation:
<lang j>coclass'Connoisseur'
isEdible=:3 :0
Line 585 ⟶ 584:
coclass'FoodBox'
create=:3 :0
collection=: 0#y
)
add=:3 :0"0
'inedible' assert
collection=: collection, y
EMPTY
)</lang>
An edible type would be a class that has a verb with the name 'eat'
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'
eat=:3 :0
Line 599 ⟶ 602:
And here is a quicky demo of the above:
<lang j>
lunch=:
a1=: conew 'Apple'
a2=: conew 'Apple'
|