Solidity changes in version 0.6.0

Tram Ho

Preamble

Solidity is a programming language familiar to blockchain application developers, used to build smart contracts. In this 0.6 version, solidity has made significant changes to support the construction of smart contracts.

Major changes

Abstract

The abstract keyword used to denote a contract is abstract (only declare the function, not implement its functions) – similar to the abstract class in Java. Contract must be declared abstract when at least 1 function is not implemented, but it is still possible to declare a contract as abstract even though all of its functions have been implemented.

Need to distinguish abstract from interface . All functions in the interface are not implemented.

For example:

Note: When a contract inherits from 1 abstract contract. and do not execute all non-implemented functions by overriding it, the contract must also be declared as abstract .

Virtual and override

A function can now only be overridden if it is declared with virtual keyword or defined in interface or abstract contract. When we want to override or change a function, we use the override keyword. For example:

In case of multiple inheritance, the original function with the same name needs to be explicitly specified after the override keyword. For example:

Note: the private function cannot be declared as virtual.

Length of array

Access to the length of arrays will always be read-only, even for storage arrays. Now we cannot change the length of the array by assigning a new value to its length. Instead, we’ll use the push and pop functions, or you can assign a whole new array to the old array.

The function push(value) for dynamic arrays no longer returns the new length of the array. Example of old usage:

Now the push function merely adds a new element to the array and returns nothing else.

Fallback function

The function without a previous name implies that the fallback function in this version will be split as fallback and receive , without the preceding function keyword. For example:

How to declare old fallback:

New declaration method:

Receive function: A contract has at least 1 receive function, using the syntax receive() external payable { ... } . This function has no parameters nor returns anything. It is called when calling the contract without any data transfer but only eth transfer. If no functions receive , it will fall into the fallback is declared as payable . Without both the receive and fallback functions with payable , the contract will not be able to accept eth and fire an exception.

Try / Catch

An external call upon failure can be captured by the try/catch as shown in the following example:

Struct and Enum:

Struct and enum can now be declared at the file level.

Address:

Cannot convert from external function type to address type. Instead, the external function type will have 1 component as the address. For example:

address(f) converts to f.address with f being the external function type.

  • It is possible to convert the address type to the address payable type using the payable (x) syntax, where x is the address type.

Conclude

Above are the noticeable changes in version 0.6.0 of Solidity. Hopefully the above article will help you in developing your Dapp.

Source:

https://solidity.readthedocs.io/en/v0.6.7/060-breaking-changes.html

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo