<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.23">
<title>Overloading</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./rouge-github.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230
</a>
</h2>
</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 <code>13</code>, <code>0w13</code>, <code>13.0</code>)
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 stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<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"><code>Int2.int</code>, <code>Int3.int</code>, &#8230;&#8203; <code>Int32.int</code>, <code>Int64.int</code>, <code>Int.int</code>, <code>IntInf.int</code>, <code>LargeInt.int</code>, <code>FixedInt.int</code>, <code>Position.int</code></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"><code>Real32.real</code>, <code>Real64.real</code>, <code>Real.real</code>, <code>LargeReal.real</code></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"><code>Word2.word</code>, <code>Word3.word</code>, &#8230;&#8203; <code>Word32.word</code>, <code>Word64.word</code>, <code>Word.word</code>, <code>LargeWord.word</code>, <code>SysWord.word</code></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 stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<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"><code>Int.int</code></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"><code>Real.real</code></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"><code>Word.word</code></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="https://smlfamily.github.io/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">
<pre class="rouge highlight"><code data-lang="sml"><span class="kr">structure</span> <span class="nn">S</span><span class="p">:</span>
   <span class="kr">sig</span>
      <span class="kr">val</span> <span class="nv">x</span><span class="p">:</span> <span class="nn">Word8</span><span class="p">.</span><span class="n">word</span>
   <span class="kr">end</span> <span class="p">=</span>
   <span class="kr">struct</span>
      <span class="kr">val</span> <span class="nv">x</span> <span class="p">=</span> <span class="mi">0w0</span>
   <span class="kr">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>The smallest enclosing structure declaration for <code>0w0</code> is
<code>val x = 0w0</code>.  Hence, <code>0w0</code> receives the default type for words,
which is <code>Word.word</code>.</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<div id="mlton-footer">
<div id="mlton-footer-text">
<div>
Last updated Thu Oct 21 15:53:06 2021 -0400 by Matthew Fluet.
<a href="https://github.com/MLton/mlton/commits/master/doc/guide/src/Overloading.adoc">Log</a>
<a href="https://github.com/MLton/mlton/edit/master/doc/guide/src/Overloading.adoc">Edit</a>
</div>
</div>
</body>
</html>