由于微信限制了第三方应用的跳转,请使用以下方法。
1. 点击右上角的
2. 选择在浏览器中打开
随着solidity 0.5.0 nightly build版本的稳步推进,正式版也将在不久的将来与开发者见面.作为一个大版本更新,新版引入了很多特性,也废弃了很多关键字,比如
下面我们将对这些改变一一予以介绍,最后给出一个示例代码,对比展示新旧版solidity代码写法的区别,供大家参考.
在之前版本的solidity编译,以下不允许的写法只会导致 warnings报警,现在将直接 errors报错.
其中 // Error...注释掉的代码在solidity版本<0.5.0均可以编译通过,但是在>=0.5.0均得换成 // Right ...的代码才能编译通过.
pragma solidity >0.4.99 <0.6;
contract A {
function () external payable {}
}
contract B {
function () external {}
}
interface ERC20 {
// function transferFrom(address _from, address _to, uint256 _value) public; // Error, `interface` must declare with `external`
function transfer(address _to, uint256 _value) external; // Right
}
contract F
{
address payable to_pay;
address to;
address owner;
// uint constant time = now; // Error, value should be compile time constant.
string constant time = "johnwick.io"; // Right
modifier onlyOwner {
require (msg.sender == owner);
_;
}
// function () { // Error, fallback function MUST be `external`
function () external { // Right
}
// function F() { // Error, consturctor function should use `constructor` keyword
// constructor() { // Error, `constructor` should be `public`
constructor () public { // Right
}
function payable_or_not() public {
bool result;
/* `address payable` VS `address` */
// to_pay = new A(); // Error, `contract` should explicitly convert to `address`
to_pay = address(new A()); // Right, fallback function of contract A is `payable`
to = address(new A()); // Right
to_pay.transfer(1); // Right, `transfer()` is member of `address payable`
result = to_pay.send(1); // Right, `send()` is member of `address payable`
to = address(new B()); // Right
// to_pay = address(new B()); // Error, fallback function of contract B is not `payable`.
// to.transfer(1 ether); // Error, `transfer()` is not member of `address`
// result = to.send(1 ether); // Error, `send()` is not member of `address`
bool success;
bytes memory data;
(success, data)= to.call.gas(0).value(1 ether)(""); // However, you can use `call("")` to send ether
address john;
john = to_pay; // Right, `address payable` can directly convert to `address`
address payable wick;
// wick = to; // Error, `address` can't directly convert to `address payable`
wick = address(uint160(to)); // Right, `address` can forcibly convert to `address payable`
wick.transfer(1 ether); // `wick` is `address payable` now
}
// struct dummy {} // Error, empty struct is not allowed.
struct yummy {string food;} // Right
// `external` function input parameter should explicitly declare `calldata` location.
function transfer(address _to, uint _value, bytes calldata _data) pure external returns (bool success){
// return; // Error, empty return statements for functions with one or more return values are now disallowed.
}
// `public` function input parameter should explicitly declare `memory` location.
function try_some(bytes memory _data) public view returns(bool) {
if(to == to_pay)
{
// throw; // Error, `throw` is deprecated.
revert(); // Right, you should use `assert(),require(),revert()` to raise an exception.
}
bytes32 _hash;
// _hash = sha3(_data); // Error, `sha3()` is deprecated.
_hash = keccak256(_data); // Right
uint256 _time;
// _time = 1 years; // Error, `years` is deprecated, due to the leap year problem.
_time = 366 days; // Right
// var secs = _time * 3600; // Error, `var` is deprecated.
uint secs; secs = _time * 0x3600; // Right
int256 _value;
// _value = 0xff wei; // Error, hex number with unit is not allowed now.
_value = 0xff*1 wei; // Right
// _value = 0Xff*1 wei; // Error, hex number prefix `0X` is not allowed now.
// _value = 1. ether; // Error, dot followed without numbers is not allowed now.
_value = 1.0 ether; // Right
// _value = +1; // Error
_value = -1;
// bytes storage not_initial_bytes; // Error, `storage` without initialization is not allowed now.
// uint[0] zero_array; // Error, fixed-size array of zero length is not allowed now.
bytes32 b32;
bytes20 b20;
bytes1 b1;
uint256 u256;
uint160 u160;
// b32 = bytes32(u160); // Error, can't directly convert `uintX` to `bytesY` of different size.
b32 = bytes32(uint256(u160)); // Right, convert to the same size, then convert to the same type.
u160 = uint160(bytes20(b32)); // Right
b32 = bytes32(u256); // Right, both are the same size, then convert to the same type.
// b1 = 255; // Error
b1 = bytes1(uint8(255+360)); // Right, decimal number like `uintX` should convert to the same size, then the same type.
// b16 = 0xff; // Error, hex number and `bytesX` are different size.
b20 = bytes20(uint160(0xff)); // Right, same size, then same type.
b1 = 0xff; // Right, hex number and `bytesX` are the same size
}
function im_not_payable() public returns (uint256 _value){
// _value = msg.value; // Error, `msg.value` MUST be used in `payable` function.
}
function im_payable() public payable returns (uint256 _value){
_value = msg.value; // Right, `msg.value` CAN be used in `payable` function.
}
// function not_impletement() public onlyOwner; // Error, function without implementation can't use `modifier`
function kill(address payable _to) public {
// suicide(_to); // Error, `suicide` is deprecated.
selfdestruct(_to); // Right
}
function scope_() view public {
uint count = 0 ;
for (uint i=1 ; i<100; i++){
count += i;
}
// i = now; // Error, not C99-Style scope
uint i; i = now; // Right, but this is error in solidity < 0.5.0 due to variable redefinition.
}
event ShowCount(uint);
function inf_loop() public {
uint count = 0;
do {
if (count!=0) {
continue;
}
count = 1; // <0.5.0, `continue` jump to this loop body, which results in infinite loop !!!
}while((count++)<10); // >=0.5.0 `continue` jump to this condition check.
emit ShowCount(count);
}
}
来源:
发布人:降维安全
声明:该文观点仅代表作者本人,不代表火讯财经立场。火讯财经系信息发布平台,仅提供信息存储空间服务。
如文章涉及侵权, 请及时致函告之,本站将第⼀时间删除⽂章。邮箱:840034348@qq.com