Skip to content

2.11. User Defined Value Types#

Syntax#


(* Introduced in 0.8.8 *)
UserDefinedValueTypeDefinition = (* type_keyword: *) TYPE_KEYWORD
(* name: *) IDENTIFIER
(* is_keyword: *) IS_KEYWORD
(* value_type: *) ElementaryType
(* semicolon: *) SEMICOLON;

User Defined Value Types#

A user defined value type allows creating a zero cost abstraction over an elementary value type. This is similar to a type alias. A user defined value type is defined using type C is V, where C is the name of the newly introduced type and V has to be a built-in value type (the underlying type).

type MyInteger is uint256;

library MyLibrary {
    function add(MyInteger a, MyInteger b) internal pure returns (MyInteger) {
        return MyInteger.wrap(MyInteger.unwrap(a) + MyInteger.unwrap(b));
    }
}

Note

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