NEWTON

NEWTON


Popular tags

    storage for array

    Asked

    3 months ago

    105

    views


    1

    This question has a bounty of $0.00 in testnet ETH. Answers to this question are eligible to win that bounty.

    These are testnet contracts. Not real ETH.

    $0.00

    Hey guys, I got this error The return type of storage variables must be a felts-only type (cannot contain pointers) when when compiling this:

    struct MultipleLinear {
        number_of_features: felt,
        coef_s: felt*,
        intercept_: felt,
    }
    @storage_var
    func solution_storage(address: felt) -> (solution: MultipleLinear) {
    }
    

    Apparently storage can only return felt? So what would be the solution if I want it to return an array or a struct that contains felt*?

      arraystoragecairo

    trangnv

    asked

    3 months ago


    1 answers

    1

    Accepted answer

    Hey,

    You can only return felt, so if you want to return an array you have to use another storage_var. For exemple if I want to return an array you may do something like this:

    struct Array {
        array_index: felt,
        len: felt,
    }
    
    @storage_var
    func get_array() -> (array: Array) {
    }
    
    @storage_var
    func array(array_index: felt, index: felt) -> (value: felt) {
    }
    

    As you can see, the storage get_array() will return a struct of type Array which contain the array_index and len of the array. Then use it in storage_var array() to get the corresponding value. Since you have the len you can create a recursion to the storage var to get all the element of the array of index array_index.

    0xC7a4...1094Da

    answered

    2 months ago

    Answer is not submitted on chain

    Your answer

    NEWTON

    NEWTON