Standard library · Numbers and collections

types

Recognising the type of a value from its text: a whole number, a decimal, a boolean or ordinary text?

import types

When data arrives from a CSV file, a form or the command line, everything is text. The types package says what kind of value a piece of text looks like, before you try to convert it. Persian and Arabic digits are recognised too.

Constants and variables

TypeKindNull

const TypeKindNull: i32 = 0
The kind of empty text.
types-KindOf.salam
import types

func main:
    each s in ["42", "3.5", "true", "hello", ""]:
        k := types.KindOf(s)
        println "[" + s + "]", k, types.KindName(k), types.IsInt(s), types.IsFloat(s), types.IsNumeric(s), types.IsBool(s)
    end
    println types.KindOf("7") == types.TypeKindInt, types.TypeKindNull, types.TypeKindBool, types.TypeKindFloat, types.TypeKindStr
end
Output[42] 2 int true false true false [3.5] 3 float false true true false [true] 1 bool false false false true [hello] 4 string false false false false [] 0 null false false false false true 0 1 3 4

TypeKindBool

const TypeKindBool: i32 = 1
The kind of a boolean, true or false.

The example for this item is shown with the ones above: types-KindOf.salam

TypeKindInt

const TypeKindInt: i32 = 2
The kind of a whole number.

The example for this item is shown with the ones above: types-KindOf.salam

TypeKindFloat

const TypeKindFloat: i32 = 3
The kind of a decimal number.

The example for this item is shown with the ones above: types-KindOf.salam

TypeKindStr

const TypeKindStr: i32 = 4
Any other text.

The example for this item is shown with the ones above: types-KindOf.salam

Functions

IsInt

func IsInt(s: str): bool
True when the text is a whole number, with or without a sign. Persian and Arabic digits are accepted.

The example for this item is shown with the ones above: types-KindOf.salam

IsFloat

func IsFloat(s: str): bool
True when the text is a decimal number, with a decimal separator or in scientific notation such as 2e5. The Persian separator ٫ is accepted. A whole number does not count here.

The example for this item is shown with the ones above: types-KindOf.salam

IsNumeric

func IsNumeric(s: str): bool
True when the text is a whole or a decimal number.

The example for this item is shown with the ones above: types-KindOf.salam

IsBool

func IsBool(s: str): bool
True when the text is true or false, in any letter case.

The example for this item is shown with the ones above: types-KindOf.salam

KindOf

func KindOf(s: str): int
Returns the kind of the text as one of the constants above.

The example for this item is shown with the ones above: types-KindOf.salam

KindName

func KindName(k: int): str
Returns the English name of a kind, such as int.

The example for this item is shown with the ones above: types-KindOf.salam

Equal

func Equal<T>(a: Vector<T>, b: Vector<T>): bool
True when two vectors have the same size and all their elements are equal in order.
types-Equal.salam
import types

func main:
    mut a := Vector {} as Vector<int>
    mut b := Vector {} as Vector<int>
    defer a.free()
    defer b.free()
    a.push(1)
    a.push(2)
    b.push(1)
    b.push(2)
    println types.Equal(a, b)
    b.push(3)
    println types.Equal(a, b)
end
Outputtrue false