4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="generator" content="AsciiDoc 8.6.9">
6 <title>Overloading</title>
7 <link rel="stylesheet" href="./asciidoc.css" type="text/css">
8 <link rel="stylesheet" href="./pygments.css" type="text/css">
11 <script type="text/javascript" src="./asciidoc.js"></script>
12 <script type="text/javascript">
17 <link rel="stylesheet" href="./mlton.css" type="text/css">
19 <body class="article">
21 <div id="banner-home">
22 <a href="./Home">MLton 20180207</a>
30 <div class="sectionbody">
31 <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>)
32 are overloaded, meaning that they can denote a constant of the
33 appropriate type as determined by context. SML defines the
34 overloading classes <em>Int</em>, <em>Real</em>, and <em>Word</em>, which denote the sets
35 of types that integer, real, and word constants may take on. In
36 MLton, these are defined as follows.</p></div>
37 <table class="tableblock frame-all grid-all"
41 <col style="width:25%;">
42 <col style="width:75%;">
45 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>
46 <td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Int2.int</span>, <span class="monospaced">Int3.int</span>, … <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>
49 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>
50 <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>
53 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>
54 <td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Word2.word</span>, <span class="monospaced">Word3.word</span>, … <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>
58 <div class="paragraph"><p>The <a href="DefinitionOfStandardML">Definition</a> allows flexibility in how
59 much context is used to resolve overloading. It says that the context
60 is <em>no larger than the smallest enclosing structure-level
61 declaration</em>, but that <em>an implementation may require that a smaller
62 context determines the type</em>. MLton uses the largest possible context
63 allowed by SML in resolving overloading. If the type of a constant is
64 not determined by context, then it takes on a default type. In MLton,
65 these are defined as follows.</p></div>
66 <table class="tableblock frame-all grid-all"
70 <col style="width:25%;">
71 <col style="width:75%;">
74 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>
75 <td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Int.int</span></p></td>
78 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>
79 <td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Real.real</span></p></td>
82 <td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>
83 <td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Word.word</span></p></td>
87 <div class="paragraph"><p>Other implementations may use a smaller context or different default
92 <h2 id="_also_see">Also see</h2>
93 <div class="sectionbody">
94 <div class="ulist"><ul>
97 <a href="http://www.standardml.org/Basis/top-level-chapter.html">discussion of overloading in the Basis Library</a>
104 <h2 id="_examples">Examples</h2>
105 <div class="sectionbody">
106 <div class="ulist"><ul>
109 The following program is rejected.
111 <div class="listingblock">
112 <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>
113 <span class="w"> </span><span class="k">sig</span><span class="w"></span>
114 <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>
115 <span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
116 <span class="w"> </span><span class="k">struct</span><span class="w"></span>
117 <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>
118 <span class="w"> </span><span class="k">end</span><span class="w"></span>
119 </pre></div></div></div>
120 <div class="paragraph"><p>The smallest enclosing structure declaration for <span class="monospaced">0w0</span> is
121 <span class="monospaced">val x = 0w0</span>. Hence, <span class="monospaced">0w0</span> receives the default type for words,
122 which is <span class="monospaced">Word.word</span>.</p></div>
128 <div id="footnotes"><hr></div>
130 <div id="footer-text">
132 <div id="footer-badges">