Aus Solidity-Tutorials:
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 { ... }
}
}
Wenn ich dies mit Trüffel kompiliere, wird eine Warnung angezeigt:
Warnung: Die Verwendung des Schlüsselworts „var“ ist veraltet.
Was ist die Alternative zum Entfernen von var, wenn Tupples von Rückgabefunktionen empfangen werden? Ich habe versucht, var zu entfernen, aber Truffle weigert sich, mit einem Fehler zu kompilieren.
Sie können die Variable zuerst mit ihrem Typ definieren und dann dem Tupel zuweisen.
function checkCounter(uint index) {
uint counter;
bool error;
(counter, error) = getCounter(index);
if (error) { }
else { }
}
Variablen müssen jetzt strikt typisiert werden. Also müssen Sie die Variablen zuerst deklarieren:
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