2

I'm trying to write a smart contract in CameLIGO that looks like this

parameters "Hello world"

storage [{ text = "Blob"; voters = ["tz1TKc3zQBMv7Rez77sX4JNdqMvG33LQrURk"]}]

with the following code

type voters = address list option

type item = {
   text : string;
   voters : voters
}

type items_storage = item list

let add_item (new_text, items_storage : string * items_storage) : items_storage =
let new_item : item = {
    text = new_text;
    voters = (* Cannot get to initialize to an empty list *)
}
in
new_item :: items_storage 

let main (p, s : string * items_storage) : operation list * items_storage =
(([] : operation list), add_item(p, s))

I don't know how to initialize an empty list.

1 Answer 1

2

If you want the voters to be optional then you need to initialize it with either the Some or None value constructors:

voters = Some ([] : address list)

or

voters = (None : address list option)

2
  • I have a Michelson error when trying your answers, I guess I might have an error in the storage : [...] At (unshown) location 5, value { "tz1TKc3zQBMv7Rez77sX4JNdqMvG33LQrURk" } is invalid for type option (list address). At (unshown) location 5, unexpected sequence, only a primitive can be used here. But I manage to make it work with : type voters = adress list and then voters = ([ ] : address list) but I don't know if it's the best solution.
    – inpace
    Dec 18, 2021 at 9:36
  • @inpace If you want voters to be optional then you actually always need to wrap it in the appropriate type constructor. So when you set it in storage you'd use voters = Some ([("tz1TKc3zQBMv7Rez77sX4JNdqMvG33LQrURk" : address)]). There's nothing wrong with having it not be optional, just depends on your needs. It would certainly be easier to go with a regular address list and leave it empty when there are no voters.
    – Nox
    Dec 18, 2021 at 13:54

Your Answer

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

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