blob: ab8c16538777d2bd4f244f023ced4c8209f113b2 [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.2.d.html" rel="prev">&lt;&lt;&nbsp;&sect;2.1.2.(d)&nbsp;Base imports</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="s2.html" rel="section">&sect;2&nbsp;Role Binding</a>&nbsp;&gt;&nbsp;<a class="nav" href="s2.1.html" rel="section">&sect;2.1&nbsp;playedBy relation</a>&nbsp;&gt;&nbsp;<a class="nav" href="s2.1.2.html" rel="section">&sect;2.1.2&nbsp;Legal base classes</a></div>
<div class="subsect depth4" id="s2.1.2.e">
<h4 class="subsect">&sect;2.1.2.(e)&nbsp;<span class="title">No free type parameters</span><a class="img" href="s2.1.2.e.html"
title="PermaLink to (e)&nbsp;No free type parameters"><img style="vertical-align:text-top;margin-left:5px;" src="../images/permalink.png"
alt="" /></a></h4>
<p>
Neither the role class nor the base class in a playedBy binding must have any <em>free type parameters</em>.
If both classes are specified with a type parameter of the same name, both parameters are identified
and are not considered as <em>free</em>.
</p>
<p>
From this follows that a role class cannot have more type parameters than its base.
Conversely, only one situation exists where a base class can have more type parameters than a role class
bound to it: if the role class has no type parameters a generic base class can be bound using
the base class's raw type, i.e., without specifying type arguments.
</p>
<div class="note">
<h5>Note:</h5>
The information from the <code>playedBy</code> declaration is used at run-time
to associate role instances to base instances.
Specifying a base class with free type parameters would imply that only such base instances
are decorated by a role whose type is conform to the specified parameterized class.
However, type arguments are not available at run-time, thus the run-time environment
is not able to decide which base instances should have a role and which should not.
This is due to the design of generics in Java which are realized by erasure.
</div>
<p>The following example shows how generics can be used in various positions. Note, that some of the concepts used in the example
will be explained in later sections.
</p>
<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> ValueTrafo<em>&lt;T&gt;</em> {</pre></td>
</tr>
<tr class="line even">
<td class="ln">2</td>
<td><pre> <b>public</b> <em>T</em> transform(<em>T</em> val) <b>throws</b> Exception { /* ... */ }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">3</td>
<td><pre>}</pre></td>
</tr>
<tr class="line even">
<td class="ln">4</td>
<td><pre><b>public</b> <b>team</b> <b>class</b> TransformTeam {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">5</td>
<td><pre> <b>protected</b> <b>class</b> SafeTrafo<em>&lt;U&gt;</em> <b>playedBy</b> ValueTrafo<em>&lt;U&gt;</em> {</pre></td>
</tr>
<tr class="line even">
<td class="ln">6</td>
<td><pre> <em>U</em> transform(<em>U</em> v) <b>-&gt;</b> <em>U</em> transform(<em>U</em> val); </pre></td>
</tr>
<tr class="line odd">
<td class="ln">7</td>
<td><pre> <b>protected</b> <em>U</em> safeTransform(<em>U</em> v) {</pre></td>
</tr>
<tr class="line even">
<td class="ln">8</td>
<td><pre> <b>try</b> {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">9</td>
<td><pre> <b>return</b> transform(v);</pre></td>
</tr>
<tr class="line even">
<td class="ln">10</td>
<td><pre> } <b>catch</b> (Exception e) {</pre></td>
</tr>
<tr class="line odd">
<td class="ln">11</td>
<td><pre> <b>return</b> v;</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> }</pre></td>
</tr>
<tr class="line odd">
<td class="ln">15</td>
<td><pre> <em>&lt;V&gt; V</em> perform(ValueTrafo<em>&lt;V&gt;</em> <b>as</b> SafeTrafo<em>&lt;V&gt;</em> trafo, <em>V</em> value) {</pre></td>
</tr>
<tr class="line even">
<td class="ln">16</td>
<td><pre> <b>return</b> trafo.safeTransform(value);</pre></td>
</tr>
<tr class="line odd">
<td class="ln">17</td>
<td><pre> } </pre></td>
</tr>
<tr class="line even">
<td class="ln">18</td>
<td><pre>}</pre></td>
</tr>
<tr class="line odd">
<td class="ln">19</td>
<td><pre>...</pre></td>
</tr>
<tr class="line even">
<td class="ln">20</td>
<td><pre>ValueTrafo<em>&lt;String&gt;</em> trafo = <b>new</b> ValueTrafo<em>&lt;String&gt;</em>();</pre></td>
</tr>
<tr class="line odd">
<td class="ln">21</td>
<td><pre>TransformTeam safeTrafo = <b>new</b> TransformTeam();</pre></td>
</tr>
<tr class="line even">
<td class="ln">22</td>
<td><pre>String s = safeTrafo.perform(trafo, "Testing");</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>Line 5 shows a role with type parameter <code>U</code> where the type parameter is identified with the
corresponding type parameter of the role's base class (which is originally declared as <code>T</code> in line 1.
</li>
<li>Line 6 shows a callout binding (<a href="s3.html" title="&sect;3&nbsp;Callout Binding" class="sect">&sect;3</a>) which mappes a base method to a corresponding role method
while maintaining the flexible typing.
</li>
<li>The regular method in lines 7-13 just passes values of type <code>U</code> around.
</li>
<li>The generic method in line 15 ff. uses declared lifting (<a href="s2.3.2.html" title="&sect;2.3.2&nbsp;Declared lifting" class="sect">&sect;2.3.2</a>) to obtain a role for a given base object.
The method has no knowledge about the concrete type arguments of either role nor base, but works under the guarantee
that both type arguments will be the same for any single invocation.
</li>
<li>Lines 20 ff. finally create instances of base and team and invoke the behavior thereby instantiating type parameters to <code>String</code>.
</li>
</ul>
</div>
</div>
<table class="nav">
<tr>
<td class="back"><a href="s2.1.2.d.html" rel="prev">&lt;&lt;&nbsp;&sect;2.1.2.(d)&nbsp;Base imports</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="s2.html" rel="section">&sect;2&nbsp;Role Binding</a>&nbsp;&gt;&nbsp;<a class="nav" href="s2.1.html" rel="section">&sect;2.1&nbsp;playedBy relation</a>&nbsp;&gt;&nbsp;<a class="nav" href="s2.1.2.html" rel="section">&sect;2.1.2&nbsp;Legal base classes</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>