0

I'm trying to write a smart contract with JSLigo. This contract has a storage type with a single string field and 2 functions, to update and reset this string:

type storage = {
    name:   string
};

type parameter =
| ["Update", string]
| ["Reset"];

type return_ = [list <operation>, storage];

let update = ([store, newValue] : [storage, string]) : storage => 
    ({...store, name: newValue});

let reset = ([store] : [storage]) : storage => 
    ({...store, name: ""});
    
    
let main = ([action, store] : [parameter, storage]) : return_ => {
 return [
   (list([]) as list <operation>),
   (match (action, {
    Update: (newValue: string) => update ([store, newValue]),
    Reset:     ()  => reset([store])}))
  ]
};

When I compile it (using https://ide.ligolang.org/), I get :

Error: File "/tmp/tmp-1WcZCXPyP3f4H.jsligo", line 15, characters 22-24:

Invalid record field "name" in record "store". 

When I write my functions like this, it's OK:

let update = ([store, newValue] : [storage, string]) : storage => {
    let newStorage : storage = {
        name: newValue
    };

    return newStorage;
}

I don't understand what is my mistake with the record and the functional update. Do you see something wrong ?

1 Answer 1

1

The problem came from the [store]:[storage] which is accepted but not well managed by the compiler. An issue has been open for this purpose. Anyway if you want your code to compile remove the opening and closing square brackets as illustrated in the following code fragment:

type storage = {
    name: string
};

// ...

let reset = (store : storage) : storage => 
    ({...store, name: ""});
       
let main = ([action, store] : [parameter, storage]) : return_ => {
 return [
   (list([]) as list <operation>),
   (match (action, {
    Update: (newValue: string) => update ([store, newValue]),
    Reset:                 ()  => reset (store)
   }))
  ]
};
1
  • Ok, I see. Thanks for the tip !
    – avl
    Mar 18, 2022 at 16:14

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.