Define a primitive data type: Difference between revisions

Line 591:
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(varInteger value) {
throw Error.@Overflow(xvalue) when xvalue is <1 or >10
} with Lookup, Show
func Integer.ToInteger() => this
 
static func TinyInt.TinyInt( as Integer x)=> {this.value
throw Error.Overflow(x) when x is <1 or >10
TinyInt.Cons(x)
}
func TinyInt.ToString() => "TinyInt (\(this.ini.value))"
func TinyInt.ToInteger() => this.value
func TinyInt + (other) => TinyInt(this.value + other.ToInteger() as Integer)
func TinyInt * (other) => TinyInt(this.value * other.ToInteger() as Integer)
func TinyInt - (other) => TinyInt(this.value - other.ToInteger() as Integer)
func TinyInt / (other) => TinyInt(this.value / other.ToInteger() as Integer)</lang>
 
Sample usage (interactive session):
Line 620 ⟶ 613:
 
dy>x * 2
Operation overflows</pre>
Runtime exception Dy601: Overflow.
Stack trace: ...</pre>
 
=={{header|E}}==
Anonymous user