<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>Overloading</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>Overloading</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>In <a href="StandardML">Standard ML</a>, constants (like <span class="monospaced">13</span>, <span class="monospaced">0w13</span>, <span class="monospaced">13.0</span>)
are overloaded, meaning that they can denote a constant of the
appropriate type as determined by context.  SML defines the
overloading classes <em>Int</em>, <em>Real</em>, and <em>Word</em>, which denote the sets
of types that integer, real, and word constants may take on.  In
MLton, these are defined as follows.</p></div>
<table class="tableblock frame-all grid-all"
style="
width:100%;
">
<col style="width:25%;">
<col style="width:75%;">
<tbody>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Int2.int</span>, <span class="monospaced">Int3.int</span>, &#8230; <span class="monospaced">Int32.int</span>, <span class="monospaced">Int64.int</span>, <span class="monospaced">Int.int</span>, <span class="monospaced">IntInf.int</span>, <span class="monospaced">LargeInt.int</span>, <span class="monospaced">FixedInt.int</span>, <span class="monospaced">Position.int</span></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Real32.real</span>, <span class="monospaced">Real64.real</span>, <span class="monospaced">Real.real</span>, <span class="monospaced">LargeReal.real</span></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Word2.word</span>, <span class="monospaced">Word3.word</span>, &#8230; <span class="monospaced">Word32.word</span>, <span class="monospaced">Word64.word</span>, <span class="monospaced">Word.word</span>, <span class="monospaced">LargeWord.word</span>, <span class="monospaced">SysWord.word</span></p></td>
</tr>
</tbody>
</table>
<div class="paragraph"><p>The <a href="DefinitionOfStandardML">Definition</a> allows flexibility in how
much context is used to resolve overloading.  It says that the context
is <em>no larger than the smallest enclosing structure-level
declaration</em>, but that <em>an implementation may require that a smaller
context determines the type</em>.  MLton uses the largest possible context
allowed by SML in resolving overloading.  If the type of a constant is
not determined by context, then it takes on a default type.  In MLton,
these are defined as follows.</p></div>
<table class="tableblock frame-all grid-all"
style="
width:100%;
">
<col style="width:25%;">
<col style="width:75%;">
<tbody>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Int.int</span></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Real.real</span></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>
<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Word.word</span></p></td>
</tr>
</tbody>
</table>
<div class="paragraph"><p>Other implementations may use a smaller context or different default
types.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="http://www.standardml.org/Basis/top-level-chapter.html">discussion of overloading in the Basis Library</a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_examples">Examples</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
The following program is rejected.
</p>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">S</span><span class="p">:</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="n">Word8</span><span class="p">.</span><span class="n">word</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0w0</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The smallest enclosing structure declaration for <span class="monospaced">0w0</span> is
<span class="monospaced">val x = 0w0</span>.  Hence, <span class="monospaced">0w0</span> receives the default type for words,
which is <span class="monospaced">Word.word</span>.</p></div>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
