智能合约语言 Solidity 教程系列3 - 函数类型

墨蓝 2022-05-29 12:06 285阅读 0赞

Solidity 教程系列第三篇 - Solidity 函数类型介绍。

写在前面

Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊、智能合约有所了解,如果你还不了解,建议你先看以太坊是什么

本文前半部分是参考Solidity 官方文档(当前最新版本:0.4.20)进行翻译,后半部分函数可见性( public, external, internal, privite )深度分析(仅针对专栏订阅用户)。

函数类型(Function Types)

函数也是一种类型,且属于值类型。
可以将一个函数赋值给一个函数类型的变量。还可以将一个函数作为参数进行传递。也可以在函数调用中返回一个函数。
函数类型有两类:内部(internal)和外部(external)函数

内部(internal)函数只能在当前合约内被调用(在当前的代码块内,包括内部库函数,和继承的函数中)。
外部(external)函数由地址和函数方法签名两部分组成,可作为外部函数调用的参数,或返回值。

函数类型定义如下:

  1. function (<parameter types>) { internal|external} [pure|constant|view|payable] [returns (<return types>)]

如果函数不需要返回,则省去returns (

有两个方式访问函数,一种是直接用函数名f, 一种是this.f, 前者用于内部函数,后者用于外部函数。

如果一个函数变量没有初始化,直接调用它将会产生异常。如果delete了一个函数后调用,也会发生同样的异常。

如果外部函数类型在Solidity的上下文环境以外的地方使用,他们会被视为function类型。它会编码为20字节的函数所在地址,和在它之前的4字节的函数方法签名一起作为bytes24类型。
合约中的public的函数,可以使用internal和external两种方式来调用。
internal访问形式为f, external访问形式为this.f

成员: 属性 selector

public (或 external) 函数有一个特殊的成员selector, 它对应一个ABI 函数选择器。
```js
pragma solidity ^0.4.16;

  1. contract Selector {
  2. function f() public view returns (bytes4) {
  3. return this.f.selector;
  4. }
  5. }
  6. ```

下面的代码显示内部(internal)函数类型的使用:

  1. pragma solidity ^0.4.16;
  2. library ArrayUtils {
  3. // internal functions can be used in internal library functions because
  4. // they will be part of the same code context
  5. function map(uint[] memory self, function (uint) pure returns (uint) f) internal pure returns (uint[] memory r) {
  6. r = new uint[](self.length);
  7. for (uint i = 0; i < self.length; i++) {
  8. r[i] = f(self[i]);
  9. }
  10. }
  11. function reduce( uint[] memory self, function (uint, uint) pure returns (uint) f ) internal pure returns (uint r) {
  12. r = self[0];
  13. for (uint i = 1; i < self.length; i++) {
  14. r = f(r, self[i]);
  15. }
  16. }
  17. function range(uint length) internal pure returns (uint[] memory r) {
  18. r = new uint[](length);
  19. for (uint i = 0; i < r.length; i++) {
  20. r[i] = i;
  21. }
  22. }
  23. }
  24. contract Pyramid {
  25. using ArrayUtils for *;
  26. function pyramid(uint l) public pure returns (uint) {
  27. return ArrayUtils.range(l).map(square).reduce(sum);
  28. }
  29. function square(uint x) internal pure returns (uint) {
  30. return x * x;
  31. }
  32. function sum(uint x, uint y) internal pure returns (uint) {
  33. return x + y;
  34. }
  35. }

下面的代码显示外部(external)函数类型的使用:

  1. pragma solidity ^0.4.11;
  2. contract Oracle {
  3. struct Request {
  4. bytes data;
  5. function(bytes memory) external callback; } Request[] requests; event NewRequest(uint); function query(bytes data, function(bytes memory) external callback) public {
  6. requests.push(Request(data, callback));
  7. NewRequest(requests.length - 1);
  8. }
  9. function reply(uint requestID, bytes response) public {
  10. // Here goes the check that the reply comes from a trusted source
  11. requests[requestID].callback(response);
  12. }
  13. }
  14. contract OracleUser {
  15. Oracle constant oracle = Oracle(0x1234567); // known contract
  16. function buySomething() {
  17. oracle.query("USD", this.oracleResponse);
  18. }
  19. function oracleResponse(bytes response) public {
  20. require(msg.sender == address(oracle));
  21. // Use the data
  22. }
  23. }

函数可见性分析

  • public - 任意访问
  • private - 仅当前合约内
  • internal - 仅当前合约及所继承的合约
  • external - 仅外部访问(在内部也只能用外部访问方式访问)

public 还是 external 最佳实践

请订阅区块链技术查看。

参考文档

Solidity官方文档-类型

深入浅出区块链 - 系统学习区块链,打造最好的区块链技术博客

转自: http://www.cnblogs.com/tinyxiong/p/8039808.html

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读