fjrojasgarcia / ethereumbook

Mastering Ethereum, by Andreas M. Antonopoulos, Gavin Wood
https://ethereumbook.info/
Other
1 stars 0 forks source link

Underscores in Solidity #19

Open fjrojasgarcia opened 6 years ago

fjrojasgarcia commented 6 years ago

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.

https://ethereum.stackexchange.com/questions/5861/are-underscores-in-modifiers-code-or-are-they-just-meant-to-look-cool