| <!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.2.html" rel="prev"><< §4.9.2 Role side inheritance</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s4.html" rel="section">§4 Callin Binding</a> > <a class="nav" href="s4.9.html" rel="section">§4.9 Callin inheritance</a></div> |
| <div class="sect depth3" id="s4.9.3"> |
| <h3 class="sect">§4.9.3 Covariant return types<a class="img" href="s4.9.3.html" |
| title="PermaLink to §4.9.3 Covariant return types"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h3> |
| <p> |
| Since version 5, Java supports the covariant redefinition of a method's return type |
| (see <a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.5" |
| class="ext">JLS 8.4.5</a>). |
| This is <em>not</em> supported for <code>callin</code> methods (<a href="#s4.9.3.a" title="§4.9.3.(a) No covariant callin methods" |
| class="sect">§4.9.3.(a)</a>). |
| If base methods with covariant redefinition of the return type are to be bound by a callin binding |
| the subsequent rules ensure that type safety is preserved. |
| Two <em>constraints</em> have to be considered: |
| |
| </p> |
| <ol> |
| <li> |
| When a callin method issues a base-call or calls its tsuper version, |
| this call must produce a value whose type is compatible to the |
| enclosing method's declared return type. |
| |
| </li> |
| <li> |
| If a replace-bound role method returns a value that is not the result of a base-call, |
| it must be ensured that the return value actually satisfies the declared signature of |
| the bound base method. |
| |
| </li> |
| </ol> |
| <div class="subsect depth4" id="s4.9.3.a"> |
| <h4 class="subsect">(a) <span class="title">No covariant callin methods</span><a class="img" href="s4.9.3.a.html" |
| title="PermaLink to (a) No covariant callin methods"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p> |
| A method declared with the <code>callin</code> modifier that overrides an inherited method |
| must not redefine the return type with respect to the inherited method. |
| This reflects that fact that an inherited callin binding should remain type-safe |
| while binding to the new, overriding role method. |
| Binding a covariant role method to the original base method would break constraint (1) above. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s4.9.3.b"> |
| <h4 class="subsect">(b) <span class="title">Capturing covariant base methods</span><a class="img" href="s4.9.3.b.html" |
| title="PermaLink to (b) Capturing covariant base methods"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p> |
| If a callin binding should indeed affect not only the specified base method |
| but also overriding versions which covariantly redefine the return type, |
| the binding must specify the base method's return type with a "+" appended |
| to the type name as in |
| </p> |
| <div class="listing plain"><pre><b>void</b> rm() <b><-</b> <b>before</b> <em>RT+</em> bm();</pre></div> |
| <p>Without the "+" sign the binding would only capture base methods whose |
| return type is exactly <code>RT</code>; |
| by appending "+" also sub-types of <code>RT</code> |
| are accepted as the declared return type. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s4.9.3.c"> |
| <h4 class="subsect">(c) <span class="title">Covariant replace binding</span><a class="img" href="s4.9.3.c.html" |
| title="PermaLink to (c) 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" |
| title="§4.9.3.(b) Capturing covariant base methods" |
| class="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><E <b>extends</b> RT> E</em> rm() <b><-</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="§4.10 Generic callin bindings" |
| class="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> <E <b>extends</b> SuperBase> 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> <E <b>extends</b> SuperBase> E ci() <b><-</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="§2.1.2.(d) Base imports" class="sect">§2.1.2.(d)</a>). |
| Here this rule is neglegted just for the purpose of keeping the example small. |
| </em></p> |
| </div> |
| </div> |
| </div> |
| <table class="nav"> |
| <tr> |
| <td class="back"><a href="s4.9.2.html" rel="prev"><< §4.9.2 Role side inheritance</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s4.html" rel="section">§4 Callin Binding</a> > <a class="nav" href="s4.9.html" rel="section">§4.9 Callin inheritance</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>© Stephan Herrmann, Christine Hundt, Marco Mosconi</address> |
| OT/J version 1.3 — last modified: 2010-06-08 |
| </div> |
| </body> |
| </html> |