Types

Type declaration

A type determines a set of values that have the same operations and functions specific for those values. A type is denoted by a type name. A map is a group of elements of one type, indexed by a set of unique string keys. By default, arr and map arrays consist of strings, but you can specify any nested types by separating them with a dot. Note that variables of types arr, map, buf, set, obj and types that has been defined with struct, unlike other types, are passed by reference, not by value. This means that if you change the value of this parameter inside the function, you will change the original variable.
1
TypeName = identifier { "." identifier }
Copied!
The Gentee language predeclares the following types.
arr bool buf char error finfo float handle int map obj range set str time trace thread
Name
Description
Values
Initial value
int
64-bit integer type
-9223372036854775808 .. 9223372036854775807
0
float
64-bit double
2.22 E–308 .. 1.79 E+308
0.0
bool
boolean type
true or false
false
str
string
a sequence of bytes
empty string
char
Unicode character
Unicode code point int32
space
arr
array
array of elements
empty array of strings
map
associative array
associative array of elements
empty associative array of strings
buf
array of bytes
array of uint8
empty array
set
set of bool
array of uint64 with 1 bit per value
empty set
obj
object
int, bool, float, str, arr.obj, map.obj
nil
handle
hidden type
any Golang type
nil
The handle type is used to pass values between built-in Golang functions. A variable of this type can contain a value of any Golang type. Go functions should keep track of the types of received values, which are described as handle in Gentee.
1
arr.map.int a
2
map.arr.str b // the same as map.arr b
3
map.bool c
4
arr.int d
Copied!

Type casting

There is no automatic type casting in Gentee . For basic types, there are functions for converting from one type to another, their names are the same as the name of the resulting type.
int
bool
str
char
float
int
int(false)
int("-23")
int('A')
int(3.24)
bool
bool(1)
bool("0")
bool(1.1)
str
str(20)
str(false)
str('z')
str(5.662)
char
float
float(10)
float("-2E-34")
1
int(false) // = 0
2
int(true) // = 1
3
bool(0) // = false
4
bool(0.) // = false
5
bool(integer except zero) // = true
6
bool("") bool("0") bool("false") //=false
7
bool("not empty, zero or false string") //=true
Copied!

Type definition

You can define a structure type by using the struct keyword. Specify a type name after the keyword, and list the field types and names inside the curly braces. All fields in a variable of a structured type are automatically initialized. All variables of these types are passed by reference, rather than by value, when passed to functions. To assign or get a field value, specify its name after the dot.
1
structDecl = "struct" identifier "{" FieldDecl { newline FieldDecl } "}"
2
FieldDecl = TypeName identifier
3
FieldExpr = PrimaryExpr "." identifier
Copied!
1
struct my : int ID; str name
2
struct myStruct {
3
int ID
4
my myval
5
arr st_arr
6
map.int st_map
7
}
8
run int {
9
myStruct ms
10
ms.ID = 20
11
return ms.ID * 2
12
}
Copied!

Function type

The Gentee language allows you to work with function identifiers. You can get the ID of the function, pass it as a parameter and call the corresponding function. To work with function identifiers, you must define a function type using the fn keyword and specify the types of parameters and return value. To get the function ID, specify &function_name.fn_type. The function identifier can be passed in parameters or assigned to a variable of the corresponding fn type. To call a function by its identifier, it is sufficient to specify the variable name and parentheses with parameters, as when calling a function by name.
The function identifier does not apply to the following functions:
    Built-in functions.
    Functions with a variable number of parameters.
    Functions with optional variables.
1
fnDecl = "fn" FnName [FnParameters] [ TypeName ]
2
FnName = identifier
3
FnParameters = "(" [ FnParameterList ] ")"
4
FnParameterList = TypeName { [","] TypeName }
5
FnIdent = "&" FuncName "." FnName
Copied!
1
fn bin( int int ) int
2
func add( int i, int j ) int : return i + j
3
func sub( int i, int j ) int : return i - j
4
func mybin(int i j, bin f ) int : return j + f(i, j)
5
6
run int {
7
bin isub = &sub.bin
8
return mybin(1, 2, &add.bin) + mybin(3, 7, isub)
9
}
Copied!
Last modified 9mo ago