2.9. Modifiers#
Syntax#
ModifierDefinition = (* modifier_keyword: *) MODIFIER_KEYWORD
(* name: *) IDENTIFIER
(* parameters: *) ParametersDeclaration?
(* attributes: *) ModifierAttributes
(* body: *) FunctionBody;
ModifierAttributes = (* item: *) ModifierAttribute*;
ModifierAttribute = (* variant: *) OverrideSpecifier (* Introduced in 0.6.0 *)
| (* variant: *) VIRTUAL_KEYWORD; (* Introduced in 0.6.0 *)
ModifierInvocation = (* name: *) IdentifierPath
(* arguments: *) ArgumentsDeclaration?;
Function Modifiers#
Function modifiers can be used to amend the semantics of functions in a declarative way:
contract MyContract {
modifier onlySeller() {
require(msg.sender == seller, "Only seller can call this.");
_; // Function body will be inserted here
}
function myFunction() public view onlySeller {
// Code here will be executed after `onlySeller` is executed.
}
}
Unlike functions, modifiers cannot be overloaded.
Note
This section is under construction. You are more than welcome to contribute suggestions to our GitHub repository.