Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): destructuring statement #964

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

novusnota
Copy link
Member

@novusnota novusnota commented Oct 16, 2024

Issue

A follow-up docs PR for issue #298 and PR #856.

Checklist

  • I have updated CHANGELOG.md
  • I have run the linter, formatter and spellchecker
  • I did not do unrelated and/or undiscussed refactorings

@novusnota novusnota added this to the v1.6.0 milestone Oct 16, 2024
@novusnota novusnota requested a review from a team as a code owner October 16, 2024 03:26
Copy link
Member

@anton-trunov anton-trunov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff, I especially liked the note about the unsupported nested structs. Let's fix one small inaccuracy and we are good to go

}
```

Destructuring assignment is exhaustive and requires specifying all the fields as variables. To deliberately ignore some of the fields, use an underscore `_{:tact}`, which would make fields value considered unused and discarded. Note, that such wildcard variable name `_{:tact}` cannot be accessed:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not exhaustive, you can say let Two { first } = s which is equivalent to let Two { second: _, first } = s (as in the example below)

Copy link
Member Author

@novusnota novusnota Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curiosly, that depends. The following example (taken from docs):

struct Two { first: Int; second: String }

fun discard(s: Two) {
    let Two { first } = s;
    // ^^^^^^^^^^^^^^^^^^^
}

Will report an error: "Cannot evaluate expression to a constant: destructuring assignment expected 2 fields, but got 1".

But if one would place the same destructuring statement in the receive() function, there would be no such error:

struct Two { first: Int; second: String }
message HasTwo { s: Two }

contract Example {
   receive() {}
   receive(msg: HasTwo) {
       // Almost the same line as in the previous example
       let Two { first } = msg.s;
       
       // No errors before or here
       dump(first);
   }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I forgot to remove this error from interpreter. I'll open a fix PR:

tact/src/interpreter.ts

Lines 1468 to 1475 in 9522f81

if (ast.identifiers.size !== Object.keys(val).length - 1) {
throwErrorConstEval(
`destructuring assignment expected ${Object.keys(val).length - 1} fields, but got ${
ast.identifiers.size
}`,
ast.loc,
);
}

anton-trunov
anton-trunov previously approved these changes Oct 23, 2024
Gusarich
Gusarich previously approved these changes Oct 25, 2024
Copy link
Member

@Gusarich Gusarich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@jeshecdom jeshecdom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the explanation. It is very clear.

docs/src/content/docs/book/statements.mdx Show resolved Hide resolved
docs/src/content/docs/book/statements.mdx Outdated Show resolved Hide resolved
docs/src/content/docs/book/statements.mdx Outdated Show resolved Hide resolved
Copy link
Contributor

@jeshecdom jeshecdom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the associated PR #969 is approved, I will approve this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants