blob: dd449615f65870acbf60d586dc28a698b4ede05e [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.4.1.html" rel="prev">&lt;&lt;&nbsp;&sect;2.4.1&nbsp;Role creation via a lifting constructor</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.4.3.html" rel="next">&sect;2.4.3&nbsp;Role creation in the presence of smart 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>&nbsp;&gt;&nbsp;<a class="nav" href="s2.4.html" rel="section">&sect;2.4&nbsp;Explicit role creation</a></div>
<div class="sect depth3" id="s2.4.2">
<h3 class="sect">&sect;2.4.2&nbsp;Role creation via a regular constructor<a class="img" href="s2.4.2.html"
title="PermaLink to &sect;2.4.2&nbsp;Role creation via a regular constructor"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h3>
<p>Roles may also be created explicitly using a custom constructor with arbitrary signature
other than the signature of the lifting constructor.<br />
Within role constructors, four kinds of self-calls are possible:
</p>
<dl>
<dt><code>base(..)</code></dt>
<dd>A constructor of the corresponding base class (<a href="sA.html#sA.5.3" title="&sect;A.5.3&nbsp;BaseCall" class="sect">&sect;A.5.3</a>(c)),
<span class="underline">unless</span> the role is involved in base class circularity (<a href="s2.1.2.b.html" title="&sect;2.1.2.(b)&nbsp;Cycles" class="sect">&sect;2.1.2.(b)</a>),
in which case a base constructor call is illegal.
</dd>
<dt><code>this(..)</code></dt>
<dd>Another constructor of the same class.</dd>
<dt><code>super(..)</code></dt>
<dd>A constructor of the super-class (normal <code>extends</code>), <span class="underline">unless</span> the super-class is bound to a different base class, in which case calling <code>super(..)</code> is not legal.
</dd>
<dt><code>tsuper(..)</code></dt>
<dd>A constructor of the corresponding role of the super-team (<a href="sA.html#sA.5.4" title="&sect;A.5.4&nbsp;TSuperCall" class="sect">&sect;A.5.4</a>(e)). Also see the constraint in <a href="s1.3.2.c.html"
title="&sect;1.3.2.(c)&nbsp;Constructors and overridden 'extends' "
class="sect">&sect;1.3.2.(c)</a>.
</dd>
</dl>
<div class="subsect depth4" id="s2.4.2.a">
<h4 class="subsect">(a)&nbsp;<span class="title">Unbound roles</span><a class="img" href="s2.4.2.a.html" title="PermaLink to (a)&nbsp;Unbound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>Each constructor of a role that is <strong>not bound</strong> to a base class must use
one of <code>this(..)</code>, <code>super(..)</code> or <code>tsuper(..)</code>.
</p>
</div>
<div class="subsect depth4" id="s2.4.2.b">
<h4 class="subsect">(b)&nbsp;<span class="title">Bound roles</span><a class="img" href="s2.4.2.b.html" title="PermaLink to (b)&nbsp;Bound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>Each constructor of a <strong>bound role</strong> must directly or indirectly invoke either
a <code>base(..)</code> constructor or a lifting constructor (see <a href="s2.3.1.html" title="&sect;2.3.1&nbsp;Implicit role creation"
class="sect">&sect;2.3.1</a>).
Indirect calls to the base constructor or lifting constructor may use any of <code>this(..)</code>, <code>super(..)</code>
or <code>tsuper(..)</code>, which simply delegates the obligation to the called constructor.
<br />
If a constructor referenced by <code>base(..)</code> is not visible according to the
regular rules of Java, it may still be called using <b>decapsulation</b> (see
also <a href="s3.4.html" title="&sect;3.4&nbsp;Overriding access restrictions"
class="sect">&sect;3.4</a>, <a href="s2.1.2.c.html" title="&sect;2.1.2.(c)&nbsp;Base class decapsulation"
class="sect">&sect;2.1.2.(c)</a>).
<br />
Note, that if the super or tsuper role is not bound, delegating the obligation to that unbound role will not work.
</p>
</div>
<div class="subsect depth4" id="s2.4.2.c">
<h4 class="subsect">(c)&nbsp;<span class="title">Super-call for bound roles</span><a class="img" href="s2.4.2.c.html"
title="PermaLink to (c)&nbsp;Super-call for bound roles"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>Instead of or prior to calling <code>base(..)</code> a constructor of a bound role explicitly or implicitly calls a super constructor.
Which constructor is applicable depends on the super role and its <code>playedBy</code> clause.
</p>
<ul>
<li>If the super role is bound to the same base class as the current role is,
<ul>
<li>not writing a super-call causes the lifting constructor of the super role to be invoked.</li>
<li>explicitly calling a super constructor requires the super constructor to <i>either</i><ol>
<li>create a role instance using a base constructor call (directly or indirectly), <i>or</i></li>
<li>be a lifting constructor receiving a base instance, which the current role must provide as the argument.</li>
</ol>
</li>
</ul>
</li>
<li>If the super role is bound but the current role refines the <code>playedBy</code>
relationship (cf. <a href="s2.1.c.html" title="&sect;2.1.(c)&nbsp;Covariant refinement"
class="sect">&sect;2.1.(c)</a>),
<ul>
<li>a lifting constructor must be called explicitly passing a base object as the argument.</li>
</ul>
</li>
<li>If the role has an explicit or implicit super role which is unbound the constructor may optionally
call a super constructor (using <code>super(..)</code> or <code>tsuper(..)</code>) prior to calling
<code>base(..)</code>. Otherwise the default constructor is implicitly invoked.
</li>
</ul>
<p>When invoking a lifting constructor of a super role the base object can optionally be obtained by using a base constructor
call as an expression:
</p>
<div class="listing plain"><pre>super(base(<i>&lt;args&gt;</i>));</pre></div>
</div>
<p>The language system evaluates the base constructor by creating an
instance of the appropriate base class using a constructor with matching
signature. Also the internal links are setup that are needed for accessing the
base object from the role and for lifting the base object to the new role
in the future.
</p>
<p>The syntax for base constructors follows the rule that role implementations
never directly refer to any names of base classes or their features.
</p>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s2.4.1.html" rel="prev">&lt;&lt;&nbsp;&sect;2.4.1&nbsp;Role creation via a lifting constructor</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.4.3.html" rel="next">&sect;2.4.3&nbsp;Role creation in the presence of smart 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>&nbsp;&gt;&nbsp;<a class="nav" href="s2.4.html" rel="section">&sect;2.4&nbsp;Explicit role creation</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>