在求解给定枚举的最小值和最大值遇到了问题

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0;

contract TestEnum { //通过enum关键字创建了一个新的类型ActionChoices enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill } //enum ActionChoices1 {1,2,3,4,5} //定义了一个ActionChoices类型的变量 ActionChoices choice; //这个自定义的变量可以带默认值,默认值是往前走 ActionChoices constant defaultChoice = ActionChoices.GoStraight;

//如何给变量赋值
function setGoStraight() public {
    choice = ActionChoices.GoStraight;
}

//和整数进行强制转换,不能隐式的转换
function getDefaultChoice() public pure returns (uint) {
    return uint(defaultChoice);//结果是2
}

//当成一个函数的返回值,这个必须要调用一下setGoStraight,这个结果才是2
function getChoice() public view returns (ActionChoices) {
    return choice;
}

function getLargestValue() public pure returns (ActionChoices) {
    return type(enum TestEnum.ActionChoices).max;
}

function getSmallestValue() public pure returns (ActionChoices) {
    return type(enum TestEnum.ActionChoices).min;
}

上述是代码,下面是遇到的错误 ParserError: Expected primary expression. --> testEnum.sol:29:21: | 29 | return type(enum TestEnum.ActionChoices).max; | ^^^^

但是直接按照文档写 type(ActionChoices).max还是有错误

}

请先 登录 后评论

1 个回答

honey_liu
请先 登录 后评论
  • 1 关注
  • 0 收藏,1014 浏览
  • tianzai 提出于 2022-09-06 14:28

相似问题