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