2

I'm having problems with liquidity syntax... (I'm using the online liquidity-lang editor).

I'm trying to make a simple smart contract that adds items with history, think of it as a maintenance log for vehicles.

I'd like to add a ton of different features, like transferability, ownership, security, I'm even considering saving small pics of the items as Byte64 strings.... but I'm stuck.

What am I missing? I get an unexplained "Syntax error" on the last line.

Here's the code that's bugging me right now...

type event = {
  date: timestamp;
  value_str: string option;
  value_int: int option;
}
type item = {
  itemName: string;
  history: event list;
}
type archive = (string, item) big_map
    (*type owner = {
        ownerName: string;
        address: address;(* To be compared with Current.source *)
        ownedItems: (string, timestamp) map;
      }*)
type storage = {
  archive: archive;
}
let%init storage = {
  archive = BigMap [];
}

let%entry main
    (item_id, item)
    storage =
  let arch = Map.add item_id item storage.archive in
  let storage = storage.archive <- arch in
  [], storage
let%entry addEvent
    (item_id, event)
    storage =
  let storage = match Map.find item_id storage.archive with
    | None ->
        failwith("Item not found", item_id)
    | Some found_id ->
        let new_history =  event :: found_id.history in
        let new_item = found_id.history <- new_history in
        let new_archive = Map.update item_id new_item storage.archive in
        let storage = storage.archive <- new_archive in
        [], storage
0

The syntax error is an unterminated let:

  let storage = match Map.find item_id storage.archive with
    | None ->
        failwith("Item not found", item_id)
    | Some found_id ->
        ...
        [], storage

It should be let storage = ... in ..., but the in ... is missing.

Let the automatic indentation be a clue.

In the OCaml syntax, you can use begin and end to delimit things when necessary:

  let storage = begin
    match Map.find item_id storage.archive with
    | None ->
        failwith("Item not found", item_id)
    | Some found_id ->
        ...
        let storage = storage.archive <- new_archive in
        storage 
  end in
  [], storage

Actually, in this case, it isn't necessary:

  let storage = match Map.find item_id storage.archive with
    | None ->
        failwith("Item not found", item_id)
    | Some found_id ->
        ...
        let storage = storage.archive <- new_archive in
        storage in
  [], storage

The ReasonML syntax might be more familiar to you. Note the "RE" button in Try-Liquidity (next to "Compile".) Example:

  let storage =
    switch (Map.find(item_id, storage.archive)) {
    | None => failwith(("Item not found", item_id))
    | Some(found_id) =>
      ...
      let storage = storage.archive = new_archive;
      storage;
    };
  ([], storage);
1
  • In the end, I did as you and others in the Riot chat said and switched to the ReasonML syntax... I occasionally have to go to the reference, hit the Try online button, convert to reason to make sure it's gonna be accepted but I'm saving on the headaches... – Alessio Giulio Corsi Apr 19 '19 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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