| <!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="s2.3.1.html" rel="prev"><< §2.3.1 Implicit role creation</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"><a href="s2.3.3.html" rel="next">§2.3.3 Smart lifting >></a></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s2.html" rel="section">§2 Role Binding</a> > <a class="nav" href="s2.3.html" rel="section">§2.3 Lifting</a></div> |
| <div class="sect depth3" id="s2.3.2"> |
| <h3 class="sect">§2.3.2 Declared lifting<a class="img" href="s2.3.2.html" |
| title="PermaLink to §2.3.2 Declared lifting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h3> |
| <div class="syntaxlink"><a href="sA.html#sA.6.2" title="§A.6.2 LiftingType" class="syntax">→ Syntax §A.6.2</a></div> |
| <div class="subsect depth4" id="s2.3.2.a"> |
| <h4 class="subsect">(a) <span class="title">Parameters with declared lifting</span><a class="img" href="s2.3.2.a.html" |
| title="PermaLink to (a) Parameters with declared lifting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>A non-static team-level method or constructor may declare a parameter with two types |
| in order to explicitly denote a place of <strong>lifting</strong>. Using the syntax |
| |
| </p> |
| <div class="listing plain"><pre><b>public</b> <b>void</b> m (BaseClass <em>as</em> RoleClass param) { <i>stmts</i> }</pre></div> |
| <p>a liftable parameter can be declared, provided the second type |
| (<code>RoleClass</code>) is a role of (<code>playedBy</code>) the first type (<code>BaseClass</code>). |
| Furthermore, the role type must be a role of the enclosing team class defining the given method. |
| The role type must be given by its simple (i.e., unqualified) name. |
| <br /> |
| Such a signature requires the caller to provide a base object (here <code>BaseClass</code>), but |
| the callee receives a role object (here <code>RoleClass</code>). |
| In fact, the client sees a signature in which the "<code>as RoleClass</code>" part is omitted. |
| <br /> |
| Compatibility between caller and callee sides is achieved by an implicitly inserted lifting translation. |
| A signature using declared lifting is only valid, if the requested lifting is possible |
| (see <a href="s2.3.3.html" title="§2.3.3 Smart lifting" class="sect">§2.3.3</a> and <a href="s2.3.4.html" title="§2.3.4 Binding ambiguities" |
| class="sect">§2.3.4</a> for details). |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.3.2.b"> |
| <h4 class="subsect">(b) <span class="title">Super in the context of declared lifting</span><a class="img" href="s2.3.2.b.html" |
| title="PermaLink to (b) Super in the context of declared lifting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>Calling <code>super</code> or <code>tsuper</code> in a method or constructor which |
| declares lifting for one or more parameters refers to a method or constructor with role type parameters, |
| i.e., lifting takes place <em>before</em> super invocation. Nevertheless, the super method may also |
| have a declared lifting signature. It will then see the same role instance(s) as the current method. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.3.2.c"> |
| <h4 class="subsect">(c) <span class="title">Declared lifting of arrays</span><a class="img" href="s2.3.2.c.html" |
| title="PermaLink to (c) Declared lifting of arrays"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>If a parameter involving explicit lifting should be of an <strong>array</strong> type, the syntax is |
| |
| </p> |
| <div class="listing plain"><pre><b>public</b> <b>void</b> m (BaseClass <b>as</b> RoleClass param[]) ...</pre></div> |
| <p>Here the brackets denoting the array apply to both types, <code>BaseClass</code> |
| and <code>RoleClass</code>. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.3.2.d"> |
| <h4 class="subsect">(d) <span class="title">Declared lifting for catch blocks</span><a class="img" href="s2.3.2.d.html" |
| title="PermaLink to (d) Declared lifting for catch blocks"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>Also the argument of a catch block may apply declared lifting like in: |
| |
| </p> |
| <div class="listing plain"><pre><b>catch</b> (BaseException <b>as</b> RoleClass param) { <i>stmts</i> }</pre></div> |
| <p>This syntax is only valid in a non-static scope of a team (directly or nested). |
| In the given example, <code>RoleClass</code> must be played by <code>BaseException</code>. |
| Note, that <code>RoleClass</code> itself need not be a throwable. |
| As the effect of this declaration the catch block will catch any exception of type <code>BaseException</code> |
| and provides it wrapped with a <code>RoleClass</code> instance to the subsequent block. |
| <br /> |
| Also note, that re-throwing the given instance <code>param</code> has the semantics of implicitly lowering |
| the role to its base exception before throwing, because the role conforms to the required type |
| <code>Throwable</code> only via lowering. |
| |
| </p> |
| </div> |
| <div class="subsect depth4" id="s2.3.2.e"> |
| <h4 class="subsect">(e) <span class="title">Generic declared lifting</span><a class="img" href="s2.3.2.e.html" |
| title="PermaLink to (e) Generic declared lifting"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png" |
| alt="" /></a></h4> |
| <p>A method with declared lifting may introduce a type parameter that is bounded relative to a given role type. |
| Such bound is declared as: |
| |
| </p> |
| <div class="listing plain"><pre><AnyBase <b>base</b> SuperRole> |
| <b>void</b> teamMethod(AnyBase <b>as</b> SuperRole arg) { |
| <span class="comment">// body using arg as of type SuperRole</span> |
| }</pre></div> |
| <p>This means that <code>AnyBase</code> is a type parameter whose instantiations must all be liftable to role <code>SuperRole</code>. |
| |
| </p> |
| <p> |
| The given type bound requires the call site to supply an argument that is compatible to any base class |
| for which the current team contains a bound role that is a sub class of <code>SuperRole</code>, including <code>SuperRole</code> itself. |
| However, <code>SuperRole</code> itself need not be bound to any base class. |
| On the other hand, different valid substitutions for <code>AnyBase</code> need not be related by inheritance. |
| |
| </p> |
| <div class="note"> |
| <h5>Note:</h5> |
| This feature supports generalized treatment of otherwise unrelated base classes. |
| This is done by defining one bound role for each base under consideration and by |
| having all these roles extend a common unbound role. |
| |
| </div> |
| </div> |
| <h5 class="listing">Example code (Declared Lifting):</h5> |
| <div class="listing example frame" id="l2.3.2"> |
| <table class="listing"> |
| <tr class="line odd"> |
| <td class="ln">1</td> |
| <td><pre><b>team</b> <b>class</b> Super {</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">2</td> |
| <td><pre> <b>public</b> <b>class</b> MyRole <b>playedBy</b> MyBase { ... }</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">3</td> |
| <td><pre> <b>void</b> m (MyRole o) { ... };</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>team</b> <b>class</b> Sub <b>extends</b> Super {</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">6</td> |
| <td><pre> <b>void</b> m (<em>MyBase <b>as</b> MyRole o</em>) {</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">7</td> |
| <td><pre> <span class="comment">// inside this method o is of type MyRole</span></pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">8</td> |
| <td><pre> super.m(o);</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">9</td> |
| <td><pre> }</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>Sub s_<b>team</b> = <b>new</b> Sub();</pre></td> |
| </tr> |
| <tr class="line even"> |
| <td class="ln">12</td> |
| <td><pre>MyBase b = <b>new</b> MyBase();</pre></td> |
| </tr> |
| <tr class="line odd"> |
| <td class="ln">13</td> |
| <td><pre>s_team.m(b); <span class="comment">// clients see a parameter "MyBase o"</span></pre></td> |
| </tr> |
| </table> |
| </div> |
| <div class="codecomment"> |
| <h5>Effects:</h5> |
| <ul> |
| <li>Clients use method <code>m</code> with a base instance (type <code>MyBase</code>) as its argument (line 13). |
| </li> |
| <li>Before executing the body of <code>m</code>, the argument is lifted such that the method body receives |
| the argument as of type <code>MyRole</code> (line 8). |
| </li> |
| </ul> |
| </div> |
| </div> |
| <table class="nav"> |
| <tr> |
| <td class="back"><a href="s2.3.1.html" rel="prev"><< §2.3.1 Implicit role creation</a></td> |
| <td class="top"><a href="index.html" rel="contents">↑ Table of Contents ↑</a></td> |
| <td class="next"><a href="s2.3.3.html" rel="next">§2.3.3 Smart lifting >></a></td> |
| </tr> |
| </table> |
| <div class="breadcrumb"><a class="nav" href="s2.html" rel="section">§2 Role Binding</a> > <a class="nav" href="s2.3.html" rel="section">§2.3 Lifting</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-05-18 |
| </div> |
| </body> |
| </html> |