At 17th April, new version has been released by Solidity community. This version introduced some significant features that will help the developers to write well optimized code. Let’s discuss them.
‘constructor’ keyword
Before this version, it was a bit difficult to segregate constructor with other functions in one glance. To make this difference, demand of introducing a new keyword was raised and after discussion it is introduced in latest version. You can go through the discussion here. A sample with new syntax is shared below:
pragma solidity ^0.4.22;
contract OwnedToken {
address owner;
bytes32 name;
// This is the constructor which assigns name.
constructor(bytes32 _name) public {
owner = msg.sender;
name = _name;
}
}
Error reason strings for revert
and require
Initially, getting the exact reason of transaction failure was a bit clumsy. New version introduced the mention of reason of error, this will help the developers in debugging and DApp users to get the exact reason of transaction failure. require
and revert
with a reason in a modifier can be looked as below:
pragma solidity ^0.4.22;
contract VendingMachine {
function buy(uint amount) payable {
if (amount > msg.value / 2 ether)
revert("Not enough Ether provided."); //with reason
// Alternative way to do it using require:
require(
amount <= msg.value / 2 ether,
"Not enough Ether provided." //reason
);
// Perform the purchase.
}
}
Some more enhancements are as :
- Some array operations got cheaper, especially the
push
function and initialization of memory arrays. - General: Add encoding routines
abi.encodePacked
,abi.encode
,abi.encodeWithSelector
andabi.encodeWithSignature
. - Added global function
gasleft()
and deprecatedmsg.gas
. - Added global function
blockhash(uint)
and deprecatedblock.hash(uint)
.
More info about solidity releases can be found here.
Solidity community is considering every small and big changes that can help developers & end-users along with making the Solidity more durable. Introduction of `emit` keyword in last version was also step ensuring the same.
Hope this helps. Thanks for reading.
EDIT 1: After 2–3 days, Solidity version 0.4.23 got released with bug fixes of last version.
EDIT2: Latest solidity version is 0.4.24. See the change-log here.
EDIT3: Read about Solidity v0.5.0 here.
Any comments, Most welcome.