Define a primitive data type: Difference between revisions

m
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
Line 534:
The code below defines a new type <code>TinyInt</code>, provides bounds checking and implementation of all standard arithmetic operators:
 
<lang dyalect>type TinyInt = {private Cons(var value)
var value
}
func Integer.toIntegerToInteger() => this
static func TinyInt.TinyInt(Integer x) {
throw "Error.Overflow"(x) when x is <1 or >10
thisTinyInt.ini.value = Cons(x)
}
func TinyInt.toStringToString() => "TinyInt (\(this.ini.value))"
func TinyInt.toIntegerToInteger() => this.ini.value
func TinyInt + (other) => TinyInt(this.ini.value + other.toIntegerToInteger())
func TinyInt * (other) => TinyInt(this.ini.value * other.toIntegerToInteger())
func TinyInt - (other) => TinyInt(this.ini.value - other.toIntegerToInteger())
func TinyInt / (other) => TinyInt(this.ini.value / other.toIntegerToInteger())</lang>
 
Sample usage (interactive session):
Anonymous user