Constrained genericity: Difference between revisions
Content added Content deleted
m (Aligned white space) |
(J) |
||
Line 252: | Line 252: | ||
<lang haskell>instance (Food a) => Eatable a where |
<lang haskell>instance (Food a) => Eatable a where |
||
eat x = munch x</lang> |
eat x = munch x</lang> |
||
=={{header|J}}== |
|||
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 |
|||
0<nc<'eat__y' |
|||
) |
|||
coclass'FoodBox' |
|||
create=:3 :0 |
|||
assert isEdible_Connoisseur_ type=:y |
|||
collection=: 0#y |
|||
) |
|||
add=:3 :0"0 |
|||
'inedible' assert type e. copath y |
|||
collection=: collection, y |
|||
)</lang> |
|||
An edible type would be a class that has a verb with the name 'eat'. For example: |
|||
<lang j>coclass'Apple' |
|||
eat=:3 :0 |
|||
smoutput'delicious' |
|||
)</lang> |
|||
And here is a quicky demo of the above: |
|||
<lang j> |
|||
lunch=:(<'Apple') conew 'FoodBox' |
|||
a1=: conew 'Apple' |
|||
a2=: conew 'Apple' |
|||
add__lunch a1 |
|||
add__lunch a2 |
|||
george=: conew 'Connoisseur' |
|||
add__lunch george |
|||
|inedible: assert</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 276: | Line 315: | ||
<lang java5>test.<EatableClass>bar();</lang> |
<lang java5>test.<EatableClass>bar();</lang> |
||
The <code>foo</code> method from before can figure out <code>T</code> from its parameter, but this <code>bar</code> method needs to be told what T is. |
The <code>foo</code> method from before can figure out <code>T</code> from its parameter, but this <code>bar</code> method needs to be told what T is. |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |