Skip to content

Commit

Permalink
small fixes from suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tnowacki committed Jul 19, 2024
1 parent ec1f0f9 commit 0c946be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions reference/src/functions/macros.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Macro Functions

Macro functions are a way of defining functions that are expanded during compilation at each call site.
The arguments of the macro are not evaluated eagerly like a normal function, and instead are
Macro functions are a way of defining functions that are expanded during compilation at each call
site. The arguments of the macro are not evaluated eagerly like a normal function, and instead are
substituted by expression. In addition, the caller can supply code to the macro via
[lambdas](#lambdas).

These expression substitution mechanics make `macro` functions similar
[to macros found in other programming languages](<https://en.wikipedia.org/wiki/Macro_(computer_science)>);
however, they are more constrained in Move than you might expect from other languages. The parameters
and return values of `macro` functions are still typed--though this can be partially relaxed with
the [`_` type](TODO). The upside of this restriction however, is that `macro` functions can be used
anywhere a normal function can be used, which is notably helpful with
however, they are more constrained in Move than you might expect from other languages. The
parameters and return values of `macro` functions are still typed--though this can be partially
relaxed with the [`_` type](TODO). The upside of this restriction however, is that `macro` functions
can be used anywhere a normal function can be used, which is notably helpful with
[method syntax](../method-syntax.md).

A more extensive
Expand Down Expand Up @@ -50,9 +50,9 @@ instead the argument expression will be substituted at each usage.

## Lambdas

Lambdas are a new type of expression that can only be used with `macro`s. These are used to pass code
from the caller into the body of the `macro`. While the substition is done at compile time, they are
used similarly to [anonymous functions](https://en.wikipedia.org/wiki/Anonymous_function),
Lambdas are a new type of expression that can only be used with `macro`s. These are used to pass
code from the caller into the body of the `macro`. While the substition is done at compile time,
they are used similarly to [anonymous functions](https://en.wikipedia.org/wiki/Anonymous_function),
[lambdas](https://en.wikipedia.org/wiki/Lambda_calculus), or
[closures](<https://en.wikipedia.org/wiki/Closure_(computer_programming)>) in other languages.

Expand Down Expand Up @@ -124,6 +124,7 @@ to a variable. For example, the following is code will produce an error:
let f = |x| 2 * x;
// ^^^^^^^^^ Error! Lambdas must be used directly in 'macro' calls
let doubled: vector<u64> = map!(vector[1, 2, 3], f);
```

## Typing

Expand Down Expand Up @@ -332,9 +333,9 @@ In the example above, the `dup` macro had a local variable `a` that was used to
`$x`. You might ask, what would happen if the variable was instead named `x`? Would that conflict
with the `x` in the lambda?

The short answer is, no. `macro` functions are [hygienic](https://en.wikipedia.org/wiki/Hygienic_macro),
meaning that the expansion of `macro`s and lambdas will not accidentally capture variables from
another scope.
The short answer is, no. `macro` functions are
[hygienic](https://en.wikipedia.org/wiki/Hygienic_macro), meaning that the expansion of `macro`s and
lambdas will not accidentally capture variables from another scope.

The compiler does this by associating a unique number with each scope. When the `macro` is expanded,
the macro body gets its own scope. Additionally, the arguments are rescoped on each usage.
Expand Down
4 changes: 2 additions & 2 deletions reference/src/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ let bar = Bar<u64, _> { x: 0, y: vector[b"hello"] };
```

The placeholder `_` may only appear in expressions and macro function definitions, not signatures.
This means you cannot use `_` as pat of the definition of a function
parameter, function return type, constant definition type, and datatype field.
This means you cannot use `_` as part of the definition of a function parameter, function return
type, constant definition type, and datatype field.

## Integers

Expand Down

0 comments on commit 0c946be

Please sign in to comment.