Estoy empezando con Solidity
y JavaScript
estoy tratando de entender Enums. tengo un contrato simple
pragma solidity ^0.4.4;
contract SimpleEnum {
enum SomeData {Channel}
SomeData sd;
function set_values(uint a) {
sd.Channel = a
return sd
}
}
Estoy tratando de averiguar cómo puedo configurar el Channel
, pero parece que no funciona.
Tiene algunos errores, así que considere leer los documentos de solidez . Además, remix es una herramienta conveniente para codificar, depurar y codificar ejercicios. Modifiqué tu código, de la siguiente manera:
pragma solidity ^0.5.0;
contract SimpleEnum {
enum SomeData {DEFAULT,ONE,TWO}
SomeData someData;
constructor() public {
someData = SomeData.DEFAULT;
}
function setValues(uint _value) public {
require(uint(SomeData.TWO) >= _value);
someData = SomeData(_value);
}
function getValue() public view returns (uint){
return uint(someData);
}
}
Espero que ayude ~
actualizar
Acerca de las enumeraciones , que se describen a continuación:
las enumeraciones son una forma de crear un tipo definido por el usuario en Solidity. Son explícitamente convertibles hacia y desde todos los tipos de enteros, pero no se permite la conversión implícita. Las conversiones explícitas verifican los rangos de valores en tiempo de ejecución y una falla provoca una excepción. Las enumeraciones necesitan al menos un miembro.
Los valores de enumeración se numeran en el orden en que se definen, comenzando en 0. Si desea obtener el valor de una enumeración, haga uint(enum_variable)
.
Consulte la documentación de enumeración ,
pragma solidity ^0.4.16;
contract test {
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.GoStraight;
function setGoStraight() public {
choice = ActionChoices.GoStraight;
}
// Since enum types are not part of the ABI, the signature of "getChoice"
// will automatically be changed to "getChoice() returns (uint8)"
// for all matters external to Solidity. The integer type used is just
// large enough to hold all enum values, i.e. if you have more values,
// `uint16` will be used and so on.
function getChoice() public view returns (ActionChoices) {
return choice;
}
function getDefaultChoice() public pure returns (uint) {
return uint(defaultChoice);
}
}
De este ejemplo oficial, resulta que la función getChoice()
devuelve el índice de esa acción. Así que supongo que enum es como una lista en python
, que convierte los valores internos al índice correspondiente.
wapití
BinGoBinBin
wapití
BinGoBinBin