Add more to types
This commit is contained in:
parent
da59ba56ce
commit
ccddf2a705
3 changed files with 63 additions and 1 deletions
24
content/types/distinct.md
Normal file
24
content/types/distinct.md
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
---
|
||||
title: Distinct Types
|
||||
---
|
||||
# Distinct Types
|
||||
|
||||
Distinct types are like other type aliases, but they provide type safety so that it is impossible to coerce a distinct type into its base type without explicit conversion.
|
||||
|
||||
``` nimrod
|
||||
type
|
||||
Dollars* = distinct float
|
||||
|
||||
var a = 20.Dollars
|
||||
a = 25 # Doesn't compile
|
||||
a = 25.Dollars # Works fine
|
||||
```
|
||||
|
||||
However, when using distinct types, none of the base type's procedures follow the type. To solve this, it is possible to create lots of procedures that basically act as a thin wrapper for the underlying types, or the `{.borrow.}` pragma can be used to automate generation of procedures.
|
||||
|
||||
``` nimrod
|
||||
proc `*` *(a, b: Dollars): Dollars {.borrow.}
|
||||
proc `+` *(a, b: Dollars): Dollars {.borrow.}
|
||||
a = 20.Dollars * 20.Dollars
|
||||
```
|
||||
<!--- XXX Uug, {.borrow: `.`.} -->
|
||||
Loading…
Add table
Add a link
Reference in a new issue