fjrojasgarcia / ethereumbook

Mastering Ethereum, by Andreas M. Antonopoulos, Gavin Wood

Home Page:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Underscores in Solidity

fjrojasgarcia opened this issue · comments

Modifiers can be used to easily change the behaviour of functions, for example to automatically check a condition prior to executing the function. They are inheritable properties of contracts and may be overridden by derived contracts.

You often see _ in modifiers

modifier onlyOwner() {
if (msg.sender != owner) throw;

From Solidity version 0.4.0+, you now need to add a semicolon after _. See Solidity - Version 0.4.0:

Change _ to _; in modifiers.

The code for the function being modified is inserted where the _ is placed in the modifier.
You can add more than one _s in the modifier code. And the code of the function being modified is inserted in each place where _ is located in the modifier. See modifier checkThree. This may be prevented by later versions of the solc compiler.
The modifiers gets called in the sequence they were defined (checkOne checkTwo checkThree) and at the end of the function, they are called in reverse. The modifiers seem to be applied like a stack. In this example anyway.