De los tutoriales de Solidity:
contract C {
uint[] counters;
function getCounter(uint index)
returns (uint counter, bool error) {
if (index >= counters.length) return (0, true);
else return (counters[index], false);
}
function checkCounter(uint index) {
var (counter, error) = getCounter(index);
if (error) { ... }
else { ... }
}
}
Si compilo esto con truffle, se muestra una advertencia:
Advertencia: el uso de la palabra clave "var" está en desuso.
¿Cuál es la alternativa para eliminar var cuando se reciben tupples de funciones de retorno? Intenté eliminar var pero truffle se niega a compilar con error.
Puede definir la variable primero con su tipo y luego asignarla a la tupla.
function checkCounter(uint index) {
uint counter;
bool error;
(counter, error) = getCounter(index);
if (error) { }
else { }
}
Las variables tienen que ser estrictamente tipeadas ahora. Así que tienes que declarar las variables primero:
contract C {
uint[] counters;
function getCounter(uint index)
returns (uint counter, bool error) {
if (index >= counters.length) return (0, true);
else return (counters[index], false);
}
function checkCounter(uint index) {
uint counter;
bool error;
(counter, error) = getCounter(index);
if (error) { ... }
else { ... }
}
}
// You have only to use solidity syntax, you can'nt mixed with javascript syntax
// 'var' is not accepted
contract Counters {
uint[] counters;
function _getCounter(uint _index) view private returns (uint, bool success) {
require(_index >= 0 && _index < counters.length);
return (counters[_index], true); // Return the value on that position in the array
}
function checkCounter(uint _index) view public returns ( ... ) {
uint counter;
bool success;
(counter, success) = _getCounter(_index);
if (success == false) { ... } else { ... };
}
}
Karan
mirg