-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e0ab22
commit 4edec9e
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.21; | ||
contract Overload { | ||
function saySomething() public pure returns(string memory){ | ||
return("Nothing"); | ||
} | ||
|
||
function saySomething(string memory something) public pure returns(string memory){ | ||
return(something); | ||
} | ||
|
||
function f(uint8 _in) public pure returns (uint8 out) { | ||
out = _in; | ||
} | ||
|
||
function f(uint256 _in) public pure returns (uint256 out) { | ||
out = _in; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: 16. 関数のオーバーロード | ||
tags: | ||
- solidity | ||
- advanced | ||
- wtfacademy | ||
- overloading | ||
--- | ||
|
||
# WTF Solidity 超シンプル入門: 16. 関数のオーバーロード | ||
|
||
最近、Solidity の学習を再開し、詳細を確認しながら「Solidity 超シンプル入門」を作っています。これは初心者向けのガイドで、プログラミングの達人向けの教材ではありません。毎週 1〜3 レッスンのペースで更新していきます。 | ||
|
||
僕のツィッターをフォローしてね:[@0xAA_Science](https://twitter.com/0xAA_Science)|[@WTFAcademy\_](https://twitter.com/WTFAcademy_) | ||
|
||
コミュニティ:[Discord](https://discord.gg/5akcruXrsk)|[Wechat](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)|[公式サイト wtf.academy](https://wtf.academy) | ||
|
||
すべてのソースコードやレッスンは github にて全公開: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity) | ||
|
||
--- | ||
|
||
## オーバーロード | ||
|
||
`Solidity`ではオーバーロードは許容されている(`overloading`)、すなわち同名の関数は、異なる関数として認識されます。特筆したいのは`Solidity`はモディファイヤー(`modifier`)のオーバーロードを許容していない点。 | ||
|
||
### 関数のオーバーロード | ||
|
||
例えば,我々は2つの`saySomething()`の関数を定義できます。片方は引数がなく、return 値は`nothing`、もう片方は`string`の引数を持っていて return 値はこの`string`の引数とします。 | ||
|
||
```solidity | ||
function saySomething() public pure returns(string memory){ | ||
return("Nothing"); | ||
} | ||
function saySomething(string memory something) public pure returns(string memory){ | ||
return(something); | ||
} | ||
``` | ||
|
||
最終的に、オーバーロード関数はコンパイラによって異なる関数セレクタ(selector)に変換されます。関数セレクタについての詳細は[WTF Solidity 超シンプル入門: 29. 関数セレクタ Selector](https://github.com/AmazingAng/WTFSolidity/tree/main/29_Selector)を参照してほしいです。 | ||
|
||
`Overloading.sol`コントラクトを例に取り、Remix でコンパイルしてデプロイした後、オーバーロード関数 `saySomething()` と `saySomething(string memory something)` をそれぞれ呼び出すと、異なる結果が返され、異なる関数として区別されることがわかります。 | ||
|
||
![16-1.jpg](./img/16-1.jpg) | ||
|
||
### 引数のマッチング(Argument Matching) | ||
|
||
オーバーロード関数を呼び出す際、入力された実際の引数と関数の引数の変数型が一致するかどうかがチェックされます。もし同じ型の複数のオーバーロード関数が存在する場合、エラーが発生します。以下の例では、`f()`という名前の関数が2つあります。一つは`uint8`型の引数を持ち、もう一つは`uint256`型の引数を持ちます。 | ||
|
||
```solidity | ||
function f(uint8 _in) public pure returns (uint8 out) { | ||
out = _in; | ||
} | ||
function f(uint256 _in) public pure returns (uint256 out) { | ||
out = _in; | ||
} | ||
``` | ||
|
||
我々は`f(50)`を呼び出すと、`50`は`uint8`にも`uint256`にも変換できるため、エラーが発生します。 | ||
|
||
## まとめ | ||
|
||
今回は、`Solidity`での関数のオーバーロードの基本的な使い方を紹介しました。 | ||
|
||
- 名前が同じでも入力パラメータの型が異なる関数は同時に存在でき、それらは異なる関数として扱われます。 |