Skip to content

3.1. Advanced Types#

Syntax#


TypeName = (* variant: *) ArrayTypeName
| (* variant: *) FunctionType
| (* variant: *) MappingType
| (* variant: *) ElementaryType
| (* variant: *) IdentifierPath;

(* Postfix unary operator *)
ArrayTypeName = (* operand: *) TypeName
(* open_bracket: *) OPEN_BRACKET
(* index: *) Expression?
(* close_bracket: *) CLOSE_BRACKET;

FunctionType = (* function_keyword: *) FUNCTION_KEYWORD
(* parameters: *) ParametersDeclaration
(* attributes: *) FunctionTypeAttributes
(* returns: *) ReturnsDeclaration?;

FunctionTypeAttributes = (* item: *) FunctionTypeAttribute*;

FunctionTypeAttribute = (* variant: *) INTERNAL_KEYWORD
| (* variant: *) EXTERNAL_KEYWORD
| (* variant: *) PRIVATE_KEYWORD
| (* variant: *) PUBLIC_KEYWORD
| (* variant: *) CONSTANT_KEYWORD (* Deprecated in 0.5.0 *)
| (* variant: *) PURE_KEYWORD (* Introduced in 0.4.16 *)
| (* variant: *) VIEW_KEYWORD (* Introduced in 0.4.16 *)
| (* variant: *) PAYABLE_KEYWORD;

MappingType = (* mapping_keyword: *) MAPPING_KEYWORD
(* open_paren: *) OPEN_PAREN
(* key_type: *) MappingKey
(* equal_greater_than: *) EQUAL_GREATER_THAN
(* value_type: *) MappingValue
(* close_paren: *) CLOSE_PAREN;

MappingKey = (* key_type: *) MappingKeyType
(* name: *) IDENTIFIER?; (* Introduced in 0.8.18 *)

MappingKeyType = (* variant: *) ElementaryType
| (* variant: *) IdentifierPath;

MappingValue = (* type_name: *) TypeName
(* name: *) IDENTIFIER?; (* Introduced in 0.8.18 *)

Function Types#

Function types are the types of functions. Variables of function type can be assigned from functions and function parameters of function type can be used to pass functions to and return functions from function calls. They come in two flavors, internal and external.

Function types are notated as follows:

function (<parameter types>) {internal|external} [pure|view|payable] [returns (<return types>)]

In contrast to the parameter types, the return types cannot be empty. If the function type should not return anything, the whole returns (<return types>) part has to be omitted.

By default, function types are internal, so the internal keyword can be omitted. Note that this only applies to function types. Visibility has to be specified explicitly for functions defined in contracts, they do not have a default.

contract Oracle {
    Request[] private requests;

    function query(bytes memory data, function(uint) external callback) public {
        requests.push(Request(data, callback));
    }

    function reply(uint requestID, uint response) public {
        requests[requestID].callback(response);
    }
}

Mapping Types#

Mapping types use the syntax mapping(_KeyType => _ValueType) and variables of mapping type are declared using the syntax mapping(_KeyType => _ValueType) _VariableName.

contract MappingExample {
    mapping(address => uint) public balances;

    function update(uint newBalance) public {
        balances[msg.sender] = newBalance;
    }
}

Note

This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.