Define a primitive data type: Difference between revisions

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 using= {
var value
}
 
func Integer.toInteger() => this
static func TinyInt.TinyInt(Integer x) {
throw "Overflow" when x is <1 or >10
private()::this.ini.value = x
}
func TinyInt.toString() => "TinyInt (\(private()::this.ini.value))"
func TinyInt.toInteger() => private()::this.ini.value
func TinyInt + (other) => TinyInt(private()::this.ini.value + other.toInteger())
func TinyInt * (other) => TinyInt(private()::this.ini.value * other.toInteger())
func TinyInt - (other) => TinyInt(private()::this.ini.value - other.toInteger())
func TinyInt / (other) => TinyInt(private()::this.ini.value / other.toInteger())</lang>
 
Sample usage (interactive session):
Anonymous user