Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / localhost / Overloading
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
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">
9
10
11 <script type="text/javascript" src="./asciidoc.js"></script>
12 <script type="text/javascript">
13 /*<![CDATA[*/
14 asciidoc.install();
15 /*]]>*/
16 </script>
17 <link rel="stylesheet" href="./mlton.css" type="text/css">
18 </head>
19 <body class="article">
20 <div id="banner">
21 <div id="banner-home">
22 <a href="./Home">MLton 20180207</a>
23 </div>
24 </div>
25 <div id="header">
26 <h1>Overloading</h1>
27 </div>
28 <div id="content">
29 <div id="preamble">
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"
38 style="
39 width:100%;
40 ">
41 <col style="width:25%;">
42 <col style="width:75%;">
43 <tbody>
44 <tr>
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>, &#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>
47 </tr>
48 <tr>
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>
51 </tr>
52 <tr>
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>, &#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>
55 </tr>
56 </tbody>
57 </table>
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"
67 style="
68 width:100%;
69 ">
70 <col style="width:25%;">
71 <col style="width:75%;">
72 <tbody>
73 <tr>
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>
76 </tr>
77 <tr>
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>
80 </tr>
81 <tr>
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>
84 </tr>
85 </tbody>
86 </table>
87 <div class="paragraph"><p>Other implementations may use a smaller context or different default
88 types.</p></div>
89 </div>
90 </div>
91 <div class="sect1">
92 <h2 id="_also_see">Also see</h2>
93 <div class="sectionbody">
94 <div class="ulist"><ul>
95 <li>
96 <p>
97 <a href="http://www.standardml.org/Basis/top-level-chapter.html">discussion of overloading in the Basis Library</a>
98 </p>
99 </li>
100 </ul></div>
101 </div>
102 </div>
103 <div class="sect1">
104 <h2 id="_examples">Examples</h2>
105 <div class="sectionbody">
106 <div class="ulist"><ul>
107 <li>
108 <p>
109 The following program is rejected.
110 </p>
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>
123 </li>
124 </ul></div>
125 </div>
126 </div>
127 </div>
128 <div id="footnotes"><hr></div>
129 <div id="footer">
130 <div id="footer-text">
131 </div>
132 <div id="footer-badges">
133 </div>
134 </div>
135 </body>
136 </html>