-
Notifications
You must be signed in to change notification settings - Fork 107
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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: |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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);
}
}
There was a problem hiding this comment.
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:
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, | |
); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this 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.
There was a problem hiding this 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.
Issue
A follow-up docs PR for issue #298 and PR #856.
Checklist