I have the following line of code in Solidity:

var res = result.toSlice();

This slices a string. When trying to compile in Truffle, I am getting var is deprecated error message. I am confused a bit, since I don't know what is the type of the variable here? Neither of my known types work here.

UPD: When changing the variable into, say, string storage, I am getting Type struct strings.slice memory is not implicitly convertible to expected type string storage pointer.

The .toSlice() function of the strings library:

     * @dev Returns a slice containing the entire string.
     * @param self The string to make a slice from.
     * @return A newly allocated slice containing the entire string.
    function toSlice(string self) internal returns (slice) {
        uint ptr;
        assembly {
            ptr := add(self, 0x20)
        return slice(bytes(self).length, ptr);

slice, apparently, is a struct:

struct slice {
        uint _len;
        uint _ptr;

2 Answers 2


Since I was using the strings library, and the variable was a struct called slice, the var is:

strings.slice memory res = result.toSlice();

var is deprecated because it's ambiguous and leaving it up to the compiler could lead to problems.

As you know, the idea here is to cast the left side with the same type as the right side.

result looks like a contract and toSlice() is a function in that contract. So, in theory this is about the return value type from toSlice().

This slices a string into a struct.

This makes me go hmmm ... a closer look at toSlice() would be helpful.

Hope it helps.



slice res = result.toSlice(...

As a style suggestion, to bring it into alignment with the style guide and to reduce confusion, consider replacing slice with SliceStruct everywhere. So

struct SliceStruct {
  • .toSlice() is a function in the strings library. I will post it.
    – Ruham
    Commented Oct 18, 2018 at 18:48
  • Please, see the updated post.
    – Ruham
    Commented Oct 18, 2018 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.