Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

TypeSizeRangeDescription
uint256 bits0 to 2^256-1Default unsigned integer
uint88 bits0 to 255Unsigned 1 byte
uint1616 bits0 to 65535Unsigned 2 bytes
uint3232 bits0 to 4294967295Unsigned 4 bytes
uint6464 bits0 to 2^64-1Unsigned 8 bytes
uint128128 bits0 to 2^128-1Unsigned 16 bytes
uint256256 bits0 to 2^256-1Unsigned 32 bytes

Literals:

{0}         (* uint256 zero *)
{42}        (* uint256 literal *)
{1000000}   (* uint256 literal *)

Signed Integers

TypeSizeRangeDescription
int256 bits-2^255 to 2^255-1Default signed integer
int88 bits-128 to 127Signed 1 byte
int1616 bits-32768 to 32767Signed 2 bytes
int3232 bits-2^31 to 2^31-1Signed 4 bytes
int6464 bits-2^63 to 2^63-1Signed 8 bytes
int128128 bits-2^127 to 2^127-1Signed 16 bytes
int256256 bits-2^255 to 2^255-1Signed 32 bytes

Literals:

{-42}       (* int256 negative *)
{100}       (* int256 positive *)

Boolean Type

TypeValuesDescription
booleantrue, falseLogical type

Literals:

@true       (* boolean true *)
@false      (* boolean false *)

Address Type

TypeDescription
addressBlockchain address (160 bits)

Literals:

{0x0000000000000000000000000000000000000000}  (* zero address *)

Special values:

msg->sender     (* sender address *)
this->address   (* contract address *)

String Type

TypeDescription
stringUTF-8 string

Literals:

{"hello"}       (* string literal *)
{""}            (* empty string *)

PhantomType

TypeDescription
PhantomTypeVoid 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