blob: 0c34a7289e4c6e9a4cadf5fb64a6ddbc62f06051 [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.3.b.html" rel="prev">&lt;&lt;&nbsp;&sect;4.9.3.(b)&nbsp;Capturing covariant base methods</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>&nbsp;&gt;&nbsp;<a class="nav" href="s4.9.html" rel="section">&sect;4.9&nbsp;Callin inheritance</a>&nbsp;&gt;&nbsp;<a class="nav" href="s4.9.3.html" rel="section">&sect;4.9.3&nbsp;Covariant return types</a></div>
<div class="subsect depth4" id="s4.9.3.c">
<h4 class="subsect">&sect;4.9.3.(c)&nbsp;<span class="title">Covariant replace binding</span><a class="img" href="s4.9.3.c.html"
title="PermaLink to (c)&nbsp;Covariant replace binding"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
When using the syntax of <a href="s4.9.3.b.html"
title="&sect;4.9.3.(b)&nbsp;Capturing covariant base methods"
class="sect">&sect;4.9.3.(b)</a> to capture base methods with
covariant return types in a callin binding with the <code>replace</code> modifier,
the role method must be specified using a free type parameter as follows:
</p>
<div class="listing plain"><pre><em>&lt;E <b>extends</b> RT&gt; E</em> rm() <b>&lt;-</b> <b>replace</b> RT+ bm();</pre></div>
<p>The role method <code>rm</code> referenced by this callin binding must use the same style
of return type using a type parameter.
The only possible non-null value of type <code>E</code>
to be returned from such method is the value provided by a base-call or a tsuper-call.<br />
This rule enforces the constraint (2) above.<br />
Note that this rule is further generalized in <a href="s4.10.html" title="&sect;4.10&nbsp;Generic callin bindings"
class="sect">&sect;4.10</a>.
</p>
<h5 class="listing">Binding a parametric role method</h5>
<div class="listing example frame">
<table class="listing">
<tr class="line odd">
<td class="ln">1</td>
<td><pre><b>public</b> <b>class</b> SuperBase {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> SuperBase foo() { <b>return</b> this; }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre> <b>void</b> check() { System.out.print("OK"); }</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre>}</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre><b>public</b> <b>class</b> SubBase <b>extends</b> SuperBase {</pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> @Override</pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> SubBase foo() { <b>return</b> this; }</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> <b>void</b> print() { System.out.print("SubBase"); }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre> String test() { </pre></td>
</tr>
<tr class="line even">
<td class="ln">10</td>
<td><pre> this.foo().print(); <span class="comment">// print() requires a SubBase</span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">11</td>
<td><pre> }</pre></td>
</tr>
<tr class="line even">
<td class="ln">12</td>
<td><pre>}</pre></td>
</tr>
<tr class="line odd">
<td class="ln">13</td>
<td><pre></pre></td>
</tr>
<tr class="line even">
<td class="ln">14</td>
<td><pre><b>public</b> <b>team</b> <b>class</b> MyTeam {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">15</td>
<td><pre> <b>protected</b> <b>class</b> R <b>playedBy</b> SuperBase {</pre></td>
</tr>
<tr class="line even">
<td class="ln">16</td>
<td><pre> <b>callin</b> &lt;E <b>extends</b> SuperBase&gt; E ci() {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">17</td>
<td><pre> E result= base.ci();</pre></td>
</tr>
<tr class="line even">
<td class="ln">18</td>
<td><pre> result.check(); <span class="comment">// check() is available on E via type bound SuperBase</span></pre></td>
</tr>
<tr class="line odd">
<td class="ln">19</td>
<td><pre> <b>return</b> result;</pre></td>
</tr>
<tr class="line even">
<td class="ln">20</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">21</td>
<td><pre> &lt;E <b>extends</b> SuperBase&gt; E ci() <b>&lt;-</b> <b>replace</b> SuperBase+ foo();</pre></td>
</tr>
<tr class="line even">
<td class="ln">22</td>
<td><pre> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">23</td>
<td><pre>}</pre></td>
</tr>
</table>
</div>
<div class="codecomment">
<h5>Explanation:</h5>
<ul>
<li>
Method <code>SubBase.foo</code> in line 7 redefines the return type
from <code>SuperBase</code> (inherited version) to <code>SubBase</code>,
thus clients like the method call in line 10 must be safe to assume
that the return value will always conform to <code>SubBase</code>.
</li>
<li>
The callin binding in line 21 explicitly captures both versions of <code>foo</code>
by specifying <code>SuperBase+</code> as the expected return type.
Thus, if an instance of <code>MyTeam</code> is active at the method call
in line 10, this call to <code>foo</code> will indeed be intercepted
even though this call is statically known to return a value of type <code>SubBase</code>.
</li>
<li>
The callin method in lines 16-20 has a return type which is not known statically,
but the return type is represented by the type variable <code>E</code>.
Since the base call is known to have the exact same signature as its enclosing
method, the value provided by the base call is of the same type <code>E</code>
and thus can be safely returned from <code>ci</code>.
<em>Note,</em> that no other non-null value is known to have the type <code>E</code>.
</li>
<li>
By specifying <code>SuperBase</code> as an upper bound for the type <code>E</code>
the callin method <code>ci</code> may invoke
any method declared in type <code>SuperBase</code>
on any value of type <code>E</code>. For an example see the call to <code>check</code>
in line 18.
</li>
</ul>
<p><em>
As an aside note that the above example uses type <code>SuperBase</code>
in an undisciplined way: within role <code>R</code> this type is bound
using <code>playedBy</code><strong> and</strong> the same type is also
used directly (as the upper bound for <code>E</code>).
This is considered bad style and it is prohibited if <code>SuperBase</code>
is imported using an base import (<a href="s2.1.2.d.html" title="&sect;2.1.2.(d)&nbsp;Base imports" class="sect">&sect;2.1.2.(d)</a>).
Here this rule is neglegted just for the purpose of keeping the example small.
</em></p>
</div>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s4.9.3.b.html" rel="prev">&lt;&lt;&nbsp;&sect;4.9.3.(b)&nbsp;Capturing covariant base methods</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>&nbsp;&gt;&nbsp;<a class="nav" href="s4.9.html" rel="section">&sect;4.9&nbsp;Callin inheritance</a>&nbsp;&gt;&nbsp;<a class="nav" href="s4.9.3.html" rel="section">&sect;4.9.3&nbsp;Covariant return types</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: 2011-05-15
</div>
</body>
</html>