How can i modify a tuple in sway?

Hi. A simple question. How can i append, pop, insert to an existing tuple ? I presume the tuple is not static (as in the sway docs it is mentioned that arrays are).

Hey @carlos! Tuples in Sway are static length type so to answer your question you cannot execute those actions.

Or use a vector instead.

Question. How can i remove an element in a vector ? Is it storage.v.remove(index) ?

Yup @carlos specifically for remove you can refer to the documentation here. Take a look at the rest of the functions on that file to see all the manipulations you can do with vecs.

Heres an example for you
Note that I am using the latest version of the toolchain

active toolchain
-----------------
latest-aarch64-apple-darwin (default)
  forc : 0.42.0
    - forc-client
      - forc-deploy : 0.42.0
      - forc-run : 0.42.0
    - forc-doc : 0.42.0
    - forc-explore : 0.28.1
    - forc-fmt : 0.42.0
    - forc-index : 0.17.3
    - forc-lsp : 0.42.0
    - forc-tx : 0.42.0
    - forc-wallet : 0.2.3
  fuel-core : 0.18.3
  fuel-indexer : 0.17.3

fuels versions
---------------
forc : 0.43
forc-wallet : 0.43.0
contract;

use std::storage::storage_vec::*;

storage {
    my_vec: StorageVec<u64> = StorageVec {},
}

abi MyContract {
    #[storage(read, write)]
    fn push_function(value: u64);

    #[storage(read, write)]
    fn remove_function(index: u64) -> u64;
}

impl MyContract for Contract {
    #[storage(read, write)]
    fn push_function(value: u64) {
        storage.my_vec.push(value);
    }

    #[storage(read, write)]
    fn remove_function(index: u64) -> u64 {
        return storage.my_vec.remove(index); 
        // Manipulating vecs (.pop(), .push(), .get(), .remove() etc.)
        // https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_vec.sw
    }
}

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.