Constrained genericity: Difference between revisions

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

edits