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

[reference] Added macro docs #70

Merged
merged 7 commits into from
Jul 22, 2024
Merged

[reference] Added macro docs #70

merged 7 commits into from
Jul 22, 2024

Conversation

tnowacki
Copy link
Contributor

No description provided.


Using the `map` macro defined above

```move
Copy link
Member

Choose a reason for hiding this comment

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

Will macros permit receiver syntax (now or ever)? If so, should we add an example of that here?

Copy link
Contributor Author

@tnowacki tnowacki Jun 28, 2024

Choose a reason for hiding this comment

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

Good call!
I need to add a section, especially since it changes things a bit for evaluation order

Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Overall looks very nice, just a couple small spelling/editing nits.

reference/src/functions/macros.md Outdated Show resolved Hide resolved
reference/src/functions/macros.md Outdated Show resolved Hide resolved
reference/src/functions/macros.md Outdated Show resolved Hide resolved
## Syntax

`macro` functions have a similar syntax to normal functions. However, all type parameter names and
all parameter names must start with a `$`.
Copy link
Contributor

Choose a reason for hiding this comment

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

"... with the exception of the _ type" possibly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I follow

Copy link

Choose a reason for hiding this comment

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

Can we have macro parameter named _ or it has to be $_ or none of these is allowed?

}
```

The `$` is there to indicate that these do not behave like their normal, non-macro counterparts. For
Copy link
Contributor

Choose a reason for hiding this comment

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

What are "these"? Might be useful to specify what exactly they are.

reference/src/functions/macros.md Outdated Show resolved Hide resolved
reference/src/functions/macros.md Outdated Show resolved Hide resolved
reference/src/functions/macros.md Outdated Show resolved Hide resolved
reference/src/functions/macros.md Outdated Show resolved Hide resolved
Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

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

Overall LGTM just a couple small editing comments.

reference/src/functions.md Outdated Show resolved Hide resolved
Comment on lines +29 to +30
For example, the following `macro` function takes a vector and a lambda, and applies the lambda to
each element of the vector to construct a new vector.
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider moving this example to the top, so someone consulting the reference sees it first thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah getting an example early might be desirable. I hope that in most renderings this shows up on the page without having to scroll, which might be eye-catching enough?

reference/src/generics.md Outdated Show resolved Hide resolved
Copy link

@awelc awelc left a comment

Choose a reason for hiding this comment

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

Small things, but otherwise LGTM

`vector::any` macro

```move
public macro fun any<$T>($v: &vector<$T>, $f: |&$T| -> bool): bool {
Copy link

Choose a reason for hiding this comment

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

It may be a little confusing that the example for the "useful" case uses labelled return whereas the "original" one uses un-labelled return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm confused. I don't know what example you are comparing this one to?

Copy link

Choose a reason for hiding this comment

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

This one uses un-labelled return:

let result = apply!(|x| { if (x == 0) return 0; x + 1 }, 100);

And this one (which in text is mentioned as a more "useful" example of the one above uses labelled return:

v.do_ref!(|e| if ($f(e)) return 'any true);

It's minor, and it's probably OK to leave as is, but when I was reading this, I expected the first example and the second one to differ only in complexity rather than them using a subtly different control flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reworded to help this


Even though `foo()` will abort, it's return type can be used to start a method call.

`$s` will not be evaluated if `$cond` is `false`. Son under a normal non-method call, an argument of
Copy link

Choose a reason for hiding this comment

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

Can't suggest and edit for some reason using GitHub machinery so here it is as text:

`$s` will not be evaluated if `$cond` is `false`, and under a normal non-method call, an argument of

maybe_s!(foo(), false) // does not abort
```

It becomes more clear as to why it does not abort when looking at the expanded form
Copy link

Choose a reason for hiding this comment

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

Why don't we move the expanded example to before line 526? Then we can say something like "Looking at the expanded form, it is clear that foo() only get executed if the condition is false, so under the following non-method call, foo() is not called and execution will not abort"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added some additional wording which I think will help


fun example() {
let mut sum = 0;
ntimes!(10, |i| sum = sum + i );
Copy link

Choose a reason for hiding this comment

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

Do we want to use a different variable name in lambda definition to avoid implying that it has to be somehow the same as what's used in the macro body?

## Syntax

`macro` functions have a similar syntax to normal functions. However, all type parameter names and
all parameter names must start with a `$`.
Copy link

Choose a reason for hiding this comment

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

Can we have macro parameter named _ or it has to be $_ or none of these is allowed?

`vector::any` macro

```move
public macro fun any<$T>($v: &vector<$T>, $f: |&$T| -> bool): bool {
Copy link

Choose a reason for hiding this comment

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

This one uses un-labelled return:

let result = apply!(|x| { if (x == 0) return 0; x + 1 }, 100);

And this one (which in text is mentioned as a more "useful" example of the one above uses labelled return:

v.do_ref!(|e| if ($f(e)) return 'any true);

It's minor, and it's probably OK to leave as is, but when I was reading this, I expected the first example and the second one to differ only in complexity rather than them using a subtly different control flow.

}
```

The reason is that if the argument if `$x` was not a reference, it would be borrowed first, which
Copy link

Choose a reason for hiding this comment

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

Suggested change
The reason is that if the argument if `$x` was not a reference, it would be borrowed first, which
The reason is that if the argument`$x` was not a reference, it would be borrowed first, which

@tnowacki tnowacki merged commit 3004cf5 into MystenLabs:main Jul 22, 2024
3 checks passed
@tnowacki tnowacki deleted the macro branch July 22, 2024 23:45
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.

5 participants