blob: 5c7b937b7fc4f2c8edee8e7f4780020a97d55e95 [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.1</title>
</head>
<body class="otdt">
<div id="content">
<table class="nav">
<tr>
<td class="back"><a id="top"></a><a href="s2.1.html" rel="prev">&lt;&lt;&nbsp;&sect;2.1&nbsp;playedBy relation</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"><a href="s2.3.html" rel="next">&sect;2.3&nbsp;Lifting&nbsp;&gt;&gt;</a></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s2.html" rel="section">&sect;2&nbsp;Role Binding</a></div>
<div class="sect depth2" id="s2.2">
<h2 class="sect">&sect;2.2&nbsp;Lowering<a class="img" href="s2.2.html" title="PermaLink to &sect;2.2&nbsp;Lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h2>
<p>Each instance of a bound role class internally stores a reference to its
base object. The reference is guaranteed to exist for each bound role
instance, and cannot be changed during its lifetime.
</p>
<div class="subsect depth3" id="s2.2.a">
<h4 class="subsect">(a)&nbsp;<span class="title">Definition of lowering</span><a class="img" href="s2.2.a.html"
title="PermaLink to (a)&nbsp;Definition of lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>Retrieving the base object from a role object is called <strong>lowering</strong>.
No other means exists for accessing the base reference.
</p>
</div>
<div class="subsect depth3" id="s2.2.b">
<h4 class="subsect">(b)&nbsp;<span class="title">Places of lowering</span><a class="img" href="s2.2.b.html"
title="PermaLink to (b)&nbsp;Places of lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>The lowering translation is not meant to be invoked
by client code, but <strong>implicit translations</strong> are inserted by
the compiler at all places where a role type is provided while the
corresponding base type (or a super type) was expected.<br />
In other words: lowering translations are inserted by the compiler at
all places in a program which would otherwise not be type correct
and which using lowering are statically type correct.
This may concern:
</p>
<ul>
<li>the right hand side of an assignment wrt. the static type of the left hand side,</li>
<li>the argument values of a method or constructor call wrt. the static type of the corresponding formal parameter,</li>
<li>the return value of a method compared to the declared return type of the method.</li>
<li>a role parameter in a callout binding (<a href="s3.3.d.html" title="&sect;3.3.(d)&nbsp;Typing rules" class="sect">&sect;3.3.(d)</a>)
</li>
<li>or the return value in a callin binding (<a href="s4.5.d.html" title="&sect;4.5.(d)&nbsp;Typing rules" class="sect">&sect;4.5.(d)</a>)
</li>
</ul>
<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> MyTeamA {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> <b>public</b> <b>class</b> <em>MyRole <b>playedBy</b> MyBase</em> { ... }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre> <b>void</b> useMyBase(<em>MyBase</em> myb) {...}</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre> <em>MyRole</em> returnMyRole() {...}</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> <b>public</b> <b>void</b> doSomething() {</pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> <em>MyRole r</em> = <b>new</b> MyRole(<b>new</b> MyBase());</pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> <em>MyBase b</em> = <em>r</em>;</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> useMyBase(<em>r</em>);</pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre> <em>MyBase b2</em> = returnMyRole();</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>}</pre></td>
</tr>
</table>
</div>
<div class="codecomment">
<h5>Effects:</h5>
<p>An instance of type <code>MyRole</code> is lowered to type <code>MyBase</code> when
</p>
<ul>
<li>assigning it to <code>b</code> (line 7)
</li>
<li>passing it as argument to a method with formal parameter of type <code>MyBase</code> (line 8)
</li>
<li>assigning the return value to a variable of type <code>MyBase</code> (line 9)
</li>
</ul>
<p><em>Note</em>: The constructor call in line 6 uses the <em>lifting constructor</em> as defined in <a href="s2.4.1.html"
title="&sect;2.4.1&nbsp;Role creation via a lifting constructor"
class="sect">&sect;2.4.1</a></p>
</div>
<p>Lowering translations are <span class="underline">not</span> inserted for
</p>
<ul>
<li>reference comparison (using <code>==</code> or <code>!=</code>)
</li>
<li><code>instanceof</code> checks
</li>
<li>cast expressions</li>
<li>return values in callout bindings <a href="s3.3.d.html" title="&sect;3.3.(d)&nbsp;Typing rules" class="sect">&sect;3.3.(d)</a>)
</li>
<li>parameters in callin bindings (<a href="s4.5.d.html" title="&sect;4.5.(d)&nbsp;Typing rules" class="sect">&sect;4.5.(d)</a>)
</li>
</ul>
<p>For cases where lowering shall be <em>forced</em> see <a href="#s2.2.d" title="&sect;2.2.(d)&nbsp;Explicit lowering" class="sect">&sect;2.2.(d)</a> below.
</p>
</div>
<div class="subsect depth3" id="s2.2.c">
<h4 class="subsect">(c)&nbsp;<span class="title">Typing</span><a class="img" href="s2.2.c.html" title="PermaLink to (c)&nbsp;Typing"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>The static type of an implicit lowering translation is the base class
declared using <code>playedBy</code> in the respective role class.
</p>
</div>
<div class="subsect depth3" id="s2.2.d">
<h4 class="subsect">(d)&nbsp;<span class="title">Explicit lowering</span><a class="img" href="s2.2.d.html"
title="PermaLink to (d)&nbsp;Explicit lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>If a base type is also the super type of its role,
which frequently happens, if a base reference is known only by
the type <code>Object</code>, lowering cannot be deduced automatically,
since a type could be interpreted both as a role type and a base type.
These cases may need <strong>explicit lowering</strong>.
For this purpose the role class must declare to implement the interface
<strong><code>ILowerable</code></strong> (from <code>org.objectteams.ITeam</code>).
This will cause the compiler to generate a method
</p>
<div class="listing plain"><pre><b>public</b> Object lower()</pre></div>
<p>for the given role class. Client code may use this method to
explicitly request the base object of a given role object.
</p>
<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> MyTeamA {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> <b>public</b> <b>class</b> MyRole <em><b>implements</b> ILowerable</em> <b>playedBy</b> MyBase { ... }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre> <b>public</b> <b>void</b> doSomething() {</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre> MyRole r = <b>new</b> MyRole(<b>new</b> MyBase());</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> Object oMyRole = r;</pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> Object oMyBase = r.<em>lower()</em>;</pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> }</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre>}</pre></td>
</tr>
</table>
</div>
</div>
<div class="subsect depth3" id="s2.2.e">
<h4 class="subsect">(e)&nbsp;<span class="title">Lowering of arrays</span><a class="img" href="s2.2.e.html"
title="PermaLink to (e)&nbsp;Lowering of arrays"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>Lowering also works for arrays of role objects.
In order to lower an array of role objects,
a new array is created and filled with base objects, one for each
role object in the original array. The array may have any number
of dimensions at any shape. The lowered array will have exactly the
same shape.<br />
Note, that each lowering translation will create a new array.
</p>
</div>
<div class="subsect depth3" id="s2.2.f">
<h4 class="subsect">(f)&nbsp;<span class="title">Ambiguous lowering</span><a class="img" href="s2.2.f.html"
title="PermaLink to (f)&nbsp;Ambiguous lowering"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>When assigning a value of a bound role type to a variable or argument of type <code>java.lang.Object</code>
this situation is considered as ambiguous lowering because the assignment could apply either (a) a direct upcast to <code>Object</code>
or (b) lowering and then upcasting.
In such situations the compiler will <em>not</em> insert a lowering translation, but a configurable warning will be issued.
</p>
</div>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s2.1.html" rel="prev">&lt;&lt;&nbsp;&sect;2.1&nbsp;playedBy relation</a></td>
<td class="top"><a href="index.html" rel="contents">&uarr;&nbsp;Table of Contents&nbsp;&uarr;</a></td>
<td class="next"><a href="s2.3.html" rel="next">&sect;2.3&nbsp;Lifting&nbsp;&gt;&gt;</a></td>
</tr>
</table>
<div class="breadcrumb"><a class="nav" href="s2.html" rel="section">&sect;2&nbsp;Role 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.1 &mdash; last modified: 2013-05-28
</div>
</body>
</html>