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 = 0import 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
endTypeKindBool
const TypeKindBool: i32 = 1true or false.The example for this item is shown with the ones above: types-KindOf.salam
TypeKindInt
const TypeKindInt: i32 = 2The example for this item is shown with the ones above: types-KindOf.salam
TypeKindFloat
const TypeKindFloat: i32 = 3The example for this item is shown with the ones above: types-KindOf.salam
TypeKindStr
const TypeKindStr: i32 = 4The example for this item is shown with the ones above: types-KindOf.salam
Functions
IsInt
func IsInt(s: str): boolThe example for this item is shown with the ones above: types-KindOf.salam
IsFloat
func IsFloat(s: str): bool2e5. 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): boolThe example for this item is shown with the ones above: types-KindOf.salam
IsBool
func IsBool(s: str): booltrue 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): intThe example for this item is shown with the ones above: types-KindOf.salam
KindName
func KindName(k: int): strint.The example for this item is shown with the ones above: types-KindOf.salam
Equal
func Equal<T>(a: Vector<T>, b: Vector<T>): boolimport 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