W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
數組切片是數組連續(xù)部分的視圖。它們寫成x[start:end], wherestart和 end是導致 uint256 類型(或隱式轉換為它)的表達式。切片的第x[start]一個元素是 ,最后一個元素是。x[end - 1]
如果start大于end或end大于數組的長度,則拋出異常。
兩者start和end都是可選的:默認start為數組的長度。0end
數組切片沒有任何成員。它們可以隱式轉換為其基礎類型的數組并支持索引訪問。索引訪問在底層數組中不是絕對的,而是相對于切片的開頭。
數組切片沒有類型名稱,這意味著沒有變量可以將數組切片作為類型,它們只存在于中間表達式中。
筆記
到目前為止,數組切片僅針對 calldata 數組實現。
數組切片對于 ABI 解碼函數參數中傳遞的輔助數據很有用:
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.5 <0.9.0; contract Proxy { /// @dev Address of the client contract managed by proxy i.e., this contract address client; constructor(address client_) { client = client_; } /// Forward call to "setOwner(address)" that is implemented by client /// after doing basic validation on the address argument. function forward(bytes calldata payload) external { bytes4 sig = bytes4(payload[:4]); // Due to truncating behaviour, bytes4(payload) performs identically. // bytes4 sig = bytes4(payload); if (sig == bytes4(keccak256("setOwner(address)"))) { address owner = abi.decode(payload[4:], (address)); require(owner != address(0), "Address of owner cannot be zero."); } (bool status,) = client.delegatecall(payload); require(status, "Forwarded call failed."); } }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: