Parametric polymorphism: Difference between revisions

Content added Content deleted
m (omit from Retro)
(added Inform 7 solution)
Line 245: Line 245:
If we have a tree of integers, i.e. <var>f</var> is <code>Tree</code> and <var>a</var> is <code>Integer</code>, then the type of <code>add1Everywhere</code> is <code>Tree Integer -> Tree Integer</code>.
If we have a tree of integers, i.e. <var>f</var> is <code>Tree</code> and <var>a</var> is <code>Integer</code>, then the type of <code>add1Everywhere</code> is <code>Tree Integer -> Tree Integer</code>.
</small></blockquote>
</small></blockquote>

=={{header|Inform 7}}==
Phrases (the equivalent of global functions) can be defined with type parameters:
<lang inform7>Polymorphism is a room.

To find (V - K) in (L - list of values of kind K):
repeat with N running from 1 to the number of entries in L:
if entry N in L is V:
say "Found [V] at entry [N] in [L].";
stop;
say "Did not find [V] in [L]."

When play begins:
find "needle" in {"parrot", "needle", "rutabaga"};
find 6 in {2, 3, 4};
end the story.</lang>

Inform 7 does not allow user-defined parametric types. Some built-in types can be parameterized, though:
<lang inform7>list of numbers
relation of texts to rooms
object based rulebook producing a number
description of things
activity on things
number valued property
text valued table column
phrase (text, text) -> number</lang>


=={{header|Java}}==
=={{header|Java}}==