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 func getInteger(x) {
private {
match x {
func getInteger(x) {
Integer => x,
match x {
TinyInt => x.toInteger(),
Integer => x,
TinyInt => x.toInteger(),
_ => throw "Type \"\(x.getType().name)\" is not supported by this operation."
_ => throw "Type \"\(x.getType():name)\" is not supported by this operation."
}
}
}
}
func boundsCheck(x) {

if x < 1 || x > 10 {
private func boundsCheck(x) {
if x < 1 || x > 10 {
throw "Overflow"
throw "Overflow."
}
x
}
}
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)