Home > Software design >  Solution for "Return argument type string storage ref is not implicitly convertible to expected
Solution for "Return argument type string storage ref is not implicitly convertible to expected

Time:07-16

I'm a newb doing my first contract with no coding experience. Can someone help with resolving this error please? The error message is: "Return argument type string storage ref is not implicitly convertible to expected type (type of first return variable) string calldata." The error is in response to the getGreetings function, which is Ln 27, Col 16 where "return message;" is.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.0;

contract GreetingsContract {
    /** This is a state variable of type string called message. This is an instance variable used to store the greetings message.
    The string data type means that the variable will contain a variable length array of characters. */
    string message;

/** This is a constructor "Greetings" with no paramater and no return value. This is public so that it can be called outside of the contract. 
Solidity lets us define a constructor that will be called only once when the contract is first deployed to the blockchain. 
The constructor does not return any value in the contract. */
    function Greetings() public {
        message =  "I'm ready!";
    }

/** This will take one parameter of type string and the name will be "_message" so we can differentiate with the internal state variable.
We'll only alter the state of the contract to overwrite the internal message with the argument. 
This function will alter the instance variable with the value sent in parameter. This is also public and doesnt return anything. 
This is often the case for functions that modify the state of the contract because there is currently no way to acces the values returned by such a function. */
    function setGreetings(string calldata _message) public {
        message = _message;
    }

/**View will return the string and the message. */
    **function getGreetings() public view returns (string calldata) {
        return message;
    }**
}

CodePudding user response:

function getGreetings() public view returns (string calldata)

calldata is a read-only data location initialized in the function input. You could return string calldata in a different scenario - if you accepted a string calldata as an input, and then returned the same value.

function foo(string calldata inputString) public pure returns (string calldata) {
    return inputString;
}

Since you're returning the value of a storage property, its value is loaded from storage to memory (not to calldata). So you need to return string memory.

function getGreetings() public view returns (string memory) {
    return message;
}
  • Related