Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / localhost / Overloading
CommitLineData
7f918cf1
CE
1<!DOCTYPE html>\r
2<html lang="en">\r
3<head>\r
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\r
5<meta name="generator" content="AsciiDoc 8.6.9">\r
6<title>Overloading</title>\r
7<link rel="stylesheet" href="./asciidoc.css" type="text/css">\r
8<link rel="stylesheet" href="./pygments.css" type="text/css">\r
9\r
10\r
11<script type="text/javascript" src="./asciidoc.js"></script>\r
12<script type="text/javascript">\r
13/*<![CDATA[*/\r
14asciidoc.install();\r
15/*]]>*/\r
16</script>\r
17<link rel="stylesheet" href="./mlton.css" type="text/css">\r
18</head>\r
19<body class="article">\r
20<div id="banner">\r
21<div id="banner-home">\r
22<a href="./Home">MLton 20180207</a>\r
23</div>\r
24</div>\r
25<div id="header">\r
26<h1>Overloading</h1>\r
27</div>\r
28<div id="content">\r
29<div id="preamble">\r
30<div class="sectionbody">\r
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>)\r
32are overloaded, meaning that they can denote a constant of the\r
33appropriate type as determined by context. SML defines the\r
34overloading classes <em>Int</em>, <em>Real</em>, and <em>Word</em>, which denote the sets\r
35of types that integer, real, and word constants may take on. In\r
36MLton, these are defined as follows.</p></div>\r
37<table class="tableblock frame-all grid-all"\r
38style="\r
39width:100%;\r
40">\r
41<col style="width:25%;">\r
42<col style="width:75%;">\r
43<tbody>\r
44<tr>\r
45<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>\r
46<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>\r
47</tr>\r
48<tr>\r
49<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>\r
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>\r
51</tr>\r
52<tr>\r
53<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>\r
54<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>\r
55</tr>\r
56</tbody>\r
57</table>\r
58<div class="paragraph"><p>The <a href="DefinitionOfStandardML">Definition</a> allows flexibility in how\r
59much context is used to resolve overloading. It says that the context\r
60is <em>no larger than the smallest enclosing structure-level\r
61declaration</em>, but that <em>an implementation may require that a smaller\r
62context determines the type</em>. MLton uses the largest possible context\r
63allowed by SML in resolving overloading. If the type of a constant is\r
64not determined by context, then it takes on a default type. In MLton,\r
65these are defined as follows.</p></div>\r
66<table class="tableblock frame-all grid-all"\r
67style="\r
68width:100%;\r
69">\r
70<col style="width:25%;">\r
71<col style="width:75%;">\r
72<tbody>\r
73<tr>\r
74<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Int</em></p></td>\r
75<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Int.int</span></p></td>\r
76</tr>\r
77<tr>\r
78<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Real</em></p></td>\r
79<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Real.real</span></p></td>\r
80</tr>\r
81<tr>\r
82<td class="tableblock halign-center valign-top" ><p class="tableblock"><em>Word</em></p></td>\r
83<td class="tableblock halign-left valign-top" ><p class="tableblock"><span class="monospaced">Word.word</span></p></td>\r
84</tr>\r
85</tbody>\r
86</table>\r
87<div class="paragraph"><p>Other implementations may use a smaller context or different default\r
88types.</p></div>\r
89</div>\r
90</div>\r
91<div class="sect1">\r
92<h2 id="_also_see">Also see</h2>\r
93<div class="sectionbody">\r
94<div class="ulist"><ul>\r
95<li>\r
96<p>\r
97<a href="http://www.standardml.org/Basis/top-level-chapter.html">discussion of overloading in the Basis Library</a>\r
98</p>\r
99</li>\r
100</ul></div>\r
101</div>\r
102</div>\r
103<div class="sect1">\r
104<h2 id="_examples">Examples</h2>\r
105<div class="sectionbody">\r
106<div class="ulist"><ul>\r
107<li>\r
108<p>\r
109The following program is rejected.\r
110</p>\r
111<div class="listingblock">\r
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>\r
113<span class="w"> </span><span class="k">sig</span><span class="w"></span>\r
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>\r
115<span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>\r
116<span class="w"> </span><span class="k">struct</span><span class="w"></span>\r
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>\r
118<span class="w"> </span><span class="k">end</span><span class="w"></span>\r
119</pre></div></div></div>\r
120<div class="paragraph"><p>The smallest enclosing structure declaration for <span class="monospaced">0w0</span> is\r
121<span class="monospaced">val x = 0w0</span>. Hence, <span class="monospaced">0w0</span> receives the default type for words,\r
122which is <span class="monospaced">Word.word</span>.</p></div>\r
123</li>\r
124</ul></div>\r
125</div>\r
126</div>\r
127</div>\r
128<div id="footnotes"><hr></div>\r
129<div id="footer">\r
130<div id="footer-text">\r
131</div>\r
132<div id="footer-badges">\r
133</div>\r
134</div>\r
135</body>\r
136</html>\r