I'm trying to implement a simple "pop" function in solidity, and I am getting very different behaviour with string values depending on whether or not I name the returns
value in the function declaration.
contract PopArray {
string[] public string_arr;
function PopArray() {
// fill the array with some data
string_arr = ['zero', 'one', 'two', 'three', 'four'];
}
// Calling this once returns 'four', calling it a second time returns 'three', etc.
function pop_string () returns (string member) {
member = string_arr[string_arr.length-1]; //get the last value
string_arr.length = string_arr.length-1; // reduce the length of the array
return member;
}
// Calling this returns nothing, but does reduce the length of the array.
function broken_pop_string () returns (string) {
string member = string_arr[string_arr.length-1];
string_arr.length = string_arr.length-1;
return member;
}
}
Here is how the results look in remix when I call one function, then the other :
Conversely, with other types I've checked, a value is returned regardless of whether the return value is named in the function declaration. Here is some code trying this out with uint
and bytes32
values.
Is there a subtlety regarding strings that I'm missing here?