| 1 |
= Overloading |
| 2 |
|
| 3 |
In <<StandardML#,Standard ML>>, constants (like `13`, `0w13`, `13.0`) |
| 4 |
are overloaded, meaning that they can denote a constant of the |
| 5 |
appropriate type as determined by context. SML defines the |
| 6 |
overloading classes _Int_, _Real_, and _Word_, which denote the sets |
| 7 |
of types that integer, real, and word constants may take on. In |
| 8 |
MLton, these are defined as follows. |
| 9 |
|
| 10 |
[cols="^25%,<75%"] |
| 11 |
|=== |
| 12 |
| _Int_ | `Int2.int`, `Int3.int`, ... `Int32.int`, `Int64.int`, `Int.int`, `IntInf.int`, `LargeInt.int`, `FixedInt.int`, `Position.int` |
| 13 |
| _Real_ | `Real32.real`, `Real64.real`, `Real.real`, `LargeReal.real` |
| 14 |
| _Word_ | `Word2.word`, `Word3.word`, ... `Word32.word`, `Word64.word`, `Word.word`, `LargeWord.word`, `SysWord.word` |
| 15 |
|=== |
| 16 |
|
| 17 |
The <<DefinitionOfStandardML#,Definition>> allows flexibility in how |
| 18 |
much context is used to resolve overloading. It says that the context |
| 19 |
is _no larger than the smallest enclosing structure-level |
| 20 |
declaration_, but that _an implementation may require that a smaller |
| 21 |
context determines the type_. MLton uses the largest possible context |
| 22 |
allowed by SML in resolving overloading. If the type of a constant is |
| 23 |
not determined by context, then it takes on a default type. In MLton, |
| 24 |
these are defined as follows. |
| 25 |
|
| 26 |
[cols="^25%,<75%"] |
| 27 |
|=== |
| 28 |
| _Int_ | `Int.int` |
| 29 |
| _Real_ | `Real.real` |
| 30 |
| _Word_ | `Word.word` |
| 31 |
|=== |
| 32 |
|
| 33 |
Other implementations may use a smaller context or different default |
| 34 |
types. |
| 35 |
|
| 36 |
== Also see |
| 37 |
|
| 38 |
* https://smlfamily.github.io/Basis/top-level-chapter.html[discussion of overloading in the Basis Library] |
| 39 |
|
| 40 |
== Examples |
| 41 |
|
| 42 |
* The following program is rejected. |
| 43 |
+ |
| 44 |
[source,sml] |
| 45 |
---- |
| 46 |
structure S: |
| 47 |
sig |
| 48 |
val x: Word8.word |
| 49 |
end = |
| 50 |
struct |
| 51 |
val x = 0w0 |
| 52 |
end |
| 53 |
---- |
| 54 |
+ |
| 55 |
The smallest enclosing structure declaration for `0w0` is |
| 56 |
`val x = 0w0`. Hence, `0w0` receives the default type for words, |
| 57 |
which is `Word.word`. |