Number
type Number = Integer | float32 | float64Number is any integer or floating-point type.
Details
intandbyteare members, as other names forint64anduint8.- Its main use is as a bound:
[T: Number]lets one generic function take every numeric type. Each call fixesTto a single member, so all theTarguments share one type and aTresult has it too. - Inside such a function, the arithmetic and comparison operators apply to
T. A literal stays anintor afloat64, not aT. - A value of type
Numberholds any number, but an operator or a function bounded byNumberneeds one numeric type: narrow the value withisfirst. - A named primitive type over a number, such as
type Meters = float64, is not aNumber.
Basic examples
A function bounded by Number takes any numeric type and returns that type:
func double[T: Number](x: T): T {
x + x
}
double(21) // 42
double(1.25) // 2.5Prelude functions such as Math.abs and Array.sum use the same bound:
Math.abs(-7) // 7
Array.sum([0.5, 0.25]) // 0.75Scope
Every integer and float type is a member, and the result keeps the argument's type:
func double[T: Number](x: T): T {
x + x
}
double(byte(100)) // 200
double(int8(-60)) // -120
double(uint64(7)) // 14
double(float32(0.75)) // 1.5An array parameter takes an array of any one numeric type:
func largest[T: Number](xs: Array[T]): T {
var best = xs[0]
for x in xs {
if x > best {
best = x
}
}
best
}
largest([3, 9, 4]) // 9
largest([float32(0.5), float32(-2.0)]) // 0.5Inside the body, is tells the members apart:
func kind[T: Number](x: T): string {
if x is float32 || x is float64 { "float" } else { "integer" }
}
kind(1) // "integer"
kind(byte(1)) // "integer"
kind(1.5) // "float"
kind(float32(2)) // "float"As a value type, Number holds numbers of different types side by side:
let readings: Array[Number] = [1, 2.5, byte(3)]
readings // [1, 2.5, 3]Narrow a Number value with is to compute with it:
func describe(n: Number): string {
if n is int {
"int ${n + 1}"
} else if n is float64 {
"float64 ${n * 2.0}"
} else {
"another number, ${n}"
}
}
describe(41) // "int 42"
describe(1.25) // "float64 2.5"
describe(int8(5)) // "another number, 5"Applications
The mean of an array of any numeric type, converted to float64 for the division:
func mean[T: Number](xs: Array[T]): float64 {
var total = 0.0
for x in xs {
total += float64(x)
}
total / float64(xs.length())
}
mean([1, 2, 4]) // 2.3333333333333335
mean([byte(10), byte(20)]) // 15.0
mean([float32(0.5), float32(1.5)]) // 1.0Check that a value lies within a range, for any numeric type:
func within[T: Number](x: T, lo: T, hi: T): bool {
x >= lo && x <= hi
}
within(5, 1, 10) // true
within(10.5, 0.0, 10.0) // false
within(byte(255), byte(0), byte(255)) // trueA record generic over its number type:
type Range[T: Number] = { lo: T, hi: T }
func Range.width[T: Number](r: Range[T]): T {
r.hi - r.lo
}
Range{ lo: 3, hi: 10 }.width() // 7
Range{ lo: 0.5, hi: 2.0 }.width() // 1.5Properties and relations
A generic body gives each member type its own arithmetic:
func double[T: Number](x: T): T {
x + x
}
assert(double(7) == 7 * 2)
assert(double(2.5) == 2.5 * 2.0)
assert(double(byte(100)) == byte(200))Math.abs is bounded by Number, so it keeps the type it gets:
assert(Math.abs(int8(-5)) == int8(5))
assert(Math.abs(float32(-1.5)) == float32(1.5))Possible issues
An operator on two Number values is a compile error, because they may be different types. Narrow them first:
let a: Number = 2
let b: Number = 3
let c = a + b
// error: + applies to a single numeric kind, string, or a named primitive type over one, not to Number; narrow first== compiles on Number values, but numbers of different types are never equal:
let a: Number = 2
let b: Number = 2.0
let c: Number = 2
a == b // false
a == c // trueis cannot test Number or Integer themselves, since an alias has no run-time identity:
func kind(n: Number): string {
if n is Integer { "integer" } else { "float" }
// error: Integer is an alias and has no run-time identity; test its members instead
}Test the members instead, as kind in Scope does.
A value typed Number cannot be passed to a function bounded by Number: T would be the whole union, not one member:
func double[T: Number](x: T): T {
x + x
}
let n: Number = 2
let d = double(n) // error: T would be Number, which is not a single member of its bound NumberAll the arguments typed T share one type, so an int and a float64 do not mix. Convert one of them:
func add[T: Number](a: T, b: T): T {
a + b
}
let s = add(1, 2.5) // error: cannot use float64 where int is expectedfunc add[T: Number](a: T, b: T): T {
a + b
}
add(float64(1), 2.5) // 3.5A literal inside the body is an int or a float64, not a T:
func half[T: Number](x: T): T {
x / 2 // error: / needs both operands of one type, got T and int
}Return a concrete type instead, and convert x to it:
func half[T: Number](x: T): float64 {
float64(x) / 2.0
}
half(7) // 3.5
half(byte(9)) // 4.5A narrowed value is a plain member type, no longer a T, so it cannot be returned as a T:
func bump[T: Number](x: T): T {
if x is int { x + 1 } else { x } // error: cannot use int where T is expected
}A string, a bool, or a named primitive type over a number is not a member:
func double[T: Number](x: T): T {
x + x
}
type Meters = float64
let e = double("ab") // error: T would be string, which is not a member of its bound Number
let g = double(Meters(1.0))
// error: T would be Meters, which is not a member of its bound Number; unwrap it with float64(...)Unwrap a named primitive type, then tag the result again:
func double[T: Number](x: T): T {
x + x
}
type Meters = float64
Meters(double(float64(Meters(1.5)))) // Meters(3.0)Arithmetic on T panics on overflow, as it does on the member type:
func add[T: Number](a: T, b: T): T {
a + b
}
add(byte(200), byte(55)) // 255
add(byte(200), byte(56)) // panic: integer overflow