blob: a5ac8492b7343453d3872b66378362bc7efffb95 [file] [log] [blame]
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "../xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="../css/ot.css" />
<link rel="stylesheet" type="text/css" href="../css/otjld.css" />
<title>OT/J Language Definition v1.3</title>
</head>
<body class="otdt">
<div id="content">
<table class="nav">
<tr>
<td class="back"><a id="top"></a><a href="s4.9.html" rel="prev">&lt;&lt;&nbsp;&sect;4.9&nbsp;Callin inheritance</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s4.html" rel="section">&sect;4&nbsp;Callin Binding</a></div>
<div class="sect depth2" id="s4.10">
<h2 class="sect">&sect;4.10&nbsp;Generic callin bindings<a class="img" href="s4.10.html"
title="PermaLink to &sect;4.10&nbsp;Generic callin bindings"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h2>
<p>
As mentioned in <a href="s4.5.d.html" title="&sect;4.5.(d)&nbsp;Typing rules" class="sect">&sect;4.5.(d)</a> replace bindings do not support subtype polymorphism
in either direction.
On the other hand, binding several base methods to the same <code>callin</code> method
may require some more flexibility if these base methods have different signatures.
This is where type parameter come to the rescue to allow for generic <code>callin</code> methods
and their binding to base methods with different signatures.<br />
Note that this rule is a generalization of rule <a href="s4.9.3.c.html" title="&sect;4.9.3.(c)&nbsp;Covariant replace binding"
class="sect">&sect;4.9.3.(c)</a>.
</p>
<p>
Additionally, any callin binding (before,replace,after) may declare one or more type parameters
for propagating type parameters of the bound base method(s) (<a href="#s4.10.e" title="&sect;4.10.(e)&nbsp;Propagating type parameters"
class="sect">&sect;4.10.(e)</a>).
</p>
<div class="subsect depth3" id="s4.10.a">
<h4 class="subsect">(a)&nbsp;<span class="title">Fresh type parameter</span><a class="img" href="s4.10.a.html"
title="PermaLink to (a)&nbsp;Fresh type parameter"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
If a callin method declares a type parameter <code>&lt;T&gt;</code> for
capturing a covariant return type
this type <code>T</code>
can be used for specifying the type of exactly one parameter or the return type.
If a type parameter is used in more than one position of a <code>callin</code> method
it is not considered a <em>fresh type parameter</em>
and can thus not be bound to a covariant return type (see <a href="#s4.10.d" title="&sect;4.10.(d)&nbsp;Binding to a type parameter"
class="sect">&sect;4.10.(d)</a>).
</p>
</div>
<div class="subsect depth3" id="s4.10.b">
<h4 class="subsect">(b)&nbsp;<span class="title">Type bounds</span><a class="img" href="s4.10.b.html" title="PermaLink to (b)&nbsp;Type bounds"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
The type parameter of a callin binding may be bounded by an upper bound as in <code>&lt;T extends C&gt;</code>.
In this case <code>T</code> can only be instantiated by types conforming to the upper bound <code>C</code>.
</p>
</div>
<div class="subsect depth3" id="s4.10.c">
<h4 class="subsect">(c)&nbsp;<span class="title">Generic replace binding</span><a class="img" href="s4.10.c.html"
title="PermaLink to (c)&nbsp;Generic replace binding"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
A generic <code>callin</code> method according to the above rules is bound using a replace
binding that declares the same number of type parameters,
where type parameters of the binding and its callin method are identified.
If the <code>callin</code> method declares bounds for its type parameters
so should the replace binding.
</p>
</div>
<div class="subsect depth3" id="s4.10.d">
<h4 class="subsect">(d)&nbsp;<span class="title">Binding to a type parameter</span><a class="img" href="s4.10.d.html"
title="PermaLink to (d)&nbsp;Binding to a type parameter"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
A fresh type parameter can be used to capture arbitrary types in the base methods to be bound.
The type parameter may be instantiated differently for each bound base method.
By such type parameter instantiation the types in role and base signatures are actually identical,
thus satisfying the requirement of two-way substitutability.
</p>
</div>
<p>
Within the body of a generic <code>callin</code> method no further rules have to be followed,
because the fresh type variable actually guarantees, that the role method cannot replace the
original value (initial argument or base-call result) with a different object,
because no type exists that is guaranteed to conform to the type parameters.
Yet, the type bound allows the role method to invoke methods of the provided object.
</p>
<h5 class="listing">Generic replace binding</h5>
<div class="listing example frame">
<table class="listing">
<tr class="line odd">
<td class="ln">1</td>
<td><pre><b>public</b> <b>team</b> <b>class</b> MyTeam {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> <b>protected</b> <b>class</b> R <b>playedBy</b> Figures {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre> <b>callin</b> &lt;E <b>extends</b> Shape, F <b>extends</b> Shape&gt; E ci(F arg) {</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre> E result= base.ci(arg);</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> <span class="error">result= arg; // illegal, types E and F are incommensurable</span></pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> <span class="error">arg= result; // illegal, types E and F are incommensurable</span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> <b>int</b> size= arg.getSize(); <span class="comment">// getSize() is availabel on F via type bound Shape</span></pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> result.resize(size); <span class="comment"> // resize() is available on E via type bound Shape</span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre> <b>return</b> result; <span class="comment">// only two legal values exist: <code>result</code> and <code>null</code></span></pre></td>
</tr>
<tr class="line even">
<td class="ln">10</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">11</td>
<td><pre> &lt;E <b>extends</b> Shape, F <b>extends</b> Shape&gt; </pre></td>
</tr>
<tr class="line even">
<td class="ln">12</td>
<td><pre> E ci(F arg) <b>&lt;-</b> <b>replace</b> Rectangle getBoundingBox(Shape original), </pre></td>
</tr>
<tr class="line odd">
<td class="ln">13</td>
<td><pre> Rectangle stretch(Square original);</pre></td>
</tr>
<tr class="line even">
<td class="ln">14</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">15</td>
<td><pre>}</pre></td>
</tr>
</table>
</div>
<div class="codecomment">
<h5>Explanation:</h5>
These declaration generate two version of the <code>callin</code> method <code>ci</code>:
<ol>
<li><code>Rectangle ci (Shape arg)</code></li>
<li><code>Rectangle ci (Square arg)</code></li>
</ol>
Within the <code>callin</code> method the following observations hold:
<ul>
<li>Line 5 is illegal for the first signature as <code>Shape</code> is not conform to <code>Rectangle</code></li>
<li>Line 6 is illegal for the second signature as <code>Rectangle</code> is not conform to <code>Square</code></li>
<li>Everything else is type-safe.</li>
</ul>
</div>
<div class="subsect depth3" id="s4.10.e">
<h4 class="subsect">(e)&nbsp;<span class="title">Propagating type parameters</span><a class="img" href="s4.10.e.html"
title="PermaLink to (e)&nbsp;Propagating type parameters"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
If a callin binding binds to a generic base method,
any type parameter(s) of the base method must be propagated into the role method
by declaring the callin binding with type parameters, too.
By matching a type parameter of a base method with a type variable
of the callin binding, this genericity is propagated through the callin binding.
</p>
<div class="listing frame">
<table class="listing">
<tr class="line odd">
<td class="ln">1</td>
<td><pre><b>class</b> MyBase {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> &lt;T&gt; T getIt(T it) { <b>return</b> it; }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre>}</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre><b>team</b> <b>class</b> MyTeam {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> <b>protected</b> <b>class</b> MyRole <b>playedBy</b> MyBase {</pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> <b>callin</b> &lt;U&gt; U rm(U a) { <b>return</b> base.rm(a); }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> &lt;U&gt; U rm(U a) <b>&lt;-</b> <b>replace</b> U getIt(U it);</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre>}</pre></td>
</tr>
</table>
</div>
<div class="codecomment">
<h5>Explanation:</h5>
The callin binding declares a type parameter <code>&lt;U&gt;</code>
which is used to match all occurrences of <code>T</code> in the signature of <code>getIt</code>.
Thus the implementation of <code>rm</code> uses the type <code>U</code>
in exactly the same generic way as <code>getIt</code> uses <code>T</code>.
</div>
</div>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s4.9.html" rel="prev">&lt;&lt;&nbsp;&sect;4.9&nbsp;Callin inheritance</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s4.html" rel="section">&sect;4&nbsp;Callin Binding</a></div>
</div>
<div id="footer">
<hr /><a class="w3c img" href="http://jigsaw.w3.org/css-validator/check/referer"
shape="rect"><img src="../images/valid-css2-blue.png" alt="Valid CSS!" height="31" width="88" /></a><a class="w3c img" href="http://validator.w3.org/check?uri=referer" shape="rect"><img src="../images/valid-xhtml10-blue.png" alt="Valid XHTML 1.0 Strict" height="31"
width="88" /></a><address>&copy; Stephan Herrmann, Christine Hundt, Marco Mosconi</address>
OT/J version 1.3 &mdash; last modified: 2010-05-18
</div>
</body>
</html>