Define a primitive data type: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 535: | Line 535: | ||
<lang dyalect>type TinyInt |
<lang dyalect>type TinyInt |
||
private |
private { |
||
func getInteger(x) { |
|||
match x { |
|||
Integer => x, |
|||
TinyInt => x.toInteger(), |
|||
_ => throw "Type \"\(x.getType() |
_ => throw "Type \"\(x.getType():name)\" is not supported by this operation." |
||
⚫ | |||
} |
} |
||
} |
|||
⚫ | |||
if x < 1 || x > 10 { |
|||
⚫ | |||
throw "Overflow" |
|||
} |
|||
x |
|||
} |
} |
||
⚫ | |||
} |
} |
||
static func TinyInt.TinyInt(i) { |
static func TinyInt.TinyInt(i) { |
||
new(boundsCheck(Integer(i))) |
new(boundsCheck(Integer(i))) |
||
} |
} |
||
func TinyInt.toString() { |
func TinyInt.toString() { |
||
valueof(this).toString() |
valueof(this).toString() |
||
} |
} |
||
func TinyInt.toInteger() { |
func TinyInt.toInteger() { |
||
valueof(this) |
valueof(this) |
||
} |
} |
||
func TinyInt + (other) { |
func TinyInt + (other) { |
||
const z = valueof(this) + getInteger(other) |
const z = valueof(this) + getInteger(other) |
||
TinyInt(z) |
TinyInt(z) |
||
} |
} |
||
func TinyInt * (other) { |
func TinyInt * (other) { |
||
const z = valueof(this) * getInteger(other) |
const z = valueof(this) * getInteger(other) |
||
TinyInt(z) |
TinyInt(z) |
||
} |
} |
||
func TinyInt - (other) { |
func TinyInt - (other) { |
||
const z = valueof(this) - getInteger(other) |
const z = valueof(this) - getInteger(other) |
||
TinyInt(z) |
TinyInt(z) |
||
} |
} |
||
func TinyInt / (other) { |
func TinyInt / (other) { |
||
const z = valueof(this) / getInteger(other) |
const z = valueof(this) / getInteger(other) |