Types, Primitives and Literals
Ursus provides a rich type system for smart contract development. This section covers all available types and their usage.
Primitive Types
Integer Types
Ursus supports signed and unsigned integers of various sizes:
Unsigned Integers
| Type | Size | Range | Description |
|---|---|---|---|
uint | 256 bits | 0 to 2^256-1 | Default unsigned integer |
uint8 | 8 bits | 0 to 255 | Unsigned 1 byte |
uint16 | 16 bits | 0 to 65535 | Unsigned 2 bytes |
uint32 | 32 bits | 0 to 4294967295 | Unsigned 4 bytes |
uint64 | 64 bits | 0 to 2^64-1 | Unsigned 8 bytes |
uint128 | 128 bits | 0 to 2^128-1 | Unsigned 16 bytes |
uint256 | 256 bits | 0 to 2^256-1 | Unsigned 32 bytes |
Literals:
{0} (* uint256 zero *)
{42} (* uint256 literal *)
{1000000} (* uint256 literal *)
Signed Integers
| Type | Size | Range | Description |
|---|---|---|---|
int | 256 bits | -2^255 to 2^255-1 | Default signed integer |
int8 | 8 bits | -128 to 127 | Signed 1 byte |
int16 | 16 bits | -32768 to 32767 | Signed 2 bytes |
int32 | 32 bits | -2^31 to 2^31-1 | Signed 4 bytes |
int64 | 64 bits | -2^63 to 2^63-1 | Signed 8 bytes |
int128 | 128 bits | -2^127 to 2^127-1 | Signed 16 bytes |
int256 | 256 bits | -2^255 to 2^255-1 | Signed 32 bytes |
Literals:
{-42} (* int256 negative *)
{100} (* int256 positive *)
Boolean Type
| Type | Values | Description |
|---|---|---|
boolean | true, false | Logical type |
Literals:
@true (* boolean true *)
@false (* boolean false *)
Address Type
| Type | Description |
|---|---|
address | Blockchain address (160 bits) |
Literals:
{0x0000000000000000000000000000000000000000} (* zero address *)
Special values:
msg->sender (* sender address *)
this->address (* contract address *)
String Type
| Type | Description |
|---|---|
string | UTF-8 string |
Literals:
{"hello"} (* string literal *)
{""} (* empty string *)
PhantomType
| Type | Description |
|---|---|
PhantomType | Void type for functions with no return value |
Usage:
Ursus Definition doSomething: UExpression PhantomType false.
{
::// ...
}
return.
Defined.
Composite Types
Mapping
Hash map from keys to values:
Syntax:
mapping keyType valueType
Examples:
mapping address uint256 (* address to balance *)
mapping uint256 boolean (* ID to flag *)
mapping address (mapping address uint256) (* nested mapping *)
Operations:
::// var balance: uint256 := balances[[addr]] . (* fetch *)
::// balances[[addr]] := {1000} . (* insert *)
::// balances->delete(addr) . (* delete *)
See also: Standard Functions - Mappings
Optional
Nullable value type:
Syntax:
optional T
Examples:
optional uint256 (* nullable uint256 *)
optional address (* nullable address *)
Operations:
::// var x: optional uint256 := some({42}) . (* create *)
::// var y: optional uint256 := none . (* empty *)
::// var has: boolean := x->hasValue() . (* check *)
::// var val: uint256 := x->get() . (* extract *)
See also: Standard Functions - Optionals
Array
Dynamic array type:
Syntax:
array T
Examples:
array uint256 (* array of numbers *)
array address (* array of addresses *)
Operations:
::// var len: uint256 := arr->length() . (* length *)
::// arr->push({42}) . (* append *)
::// var last: uint256 := arr->pop() . (* remove last *)
::// var elem: uint256 := arr[[{0}]] . (* index *)
See also: Standard Functions - Arrays
User-Defined Types
Records (Structs)
Define custom struct types:
Syntax:
Record TypeName := {
field1: type1;
field2: type2;
...
fieldN: typeN
}.
Example:
Types
Record User := {
user_id: uint256;
balance: uint256;
active: boolean
};
Literals:
[$ {1} ⇒ user_id ;
{1000} ⇒ balance ;
@true ⇒ active $]
Field access:
::// var id: uint256 := user->user_id .
::// user->balance := {2000} .
See also: Structures
Enumerations
Define enumeration types:
Example:
Types
Inductive Status := Pending | Active | Completed;
Usage:
::// var status: Status := Active .
::// match status with
| Pending => { ->> }
| Active => { ->> }
| Completed => { ->> }
end | .
Type Resolution
_ResolveName
Reference user-defined types by name:
Syntax:
(_ResolveName "TypeName")
Example:
Record Contract := {
current_user: (_ResolveName "User");
users: mapping address (_ResolveName "User")
}.
_ResolveRecord
Reference record types:
Syntax:
(_ResolveRecord "RecordName")
Type Conversions
Explicit Conversions
uint8_to_uint256(x) (* uint8 → uint256 *)
uint256_to_uint8(x) (* uint256 → uint8 (truncate) *)
int_to_uint(x) (* int → uint (reinterpret) *)
Implicit Conversions
Ursus performs some implicit conversions:
- Smaller unsigned to larger unsigned
- Literals to appropriate types
See Also
- Structures - User-defined types
- Standard Functions - Type operations
- Contract Structure - Using types in contracts