Skip to content

Commit

Permalink
Add explanation about trailing macro
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Aug 1, 2024
1 parent 414d7b2 commit c6ed827
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/content/docs/references/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,32 @@ while C3 isn't the best, no real better alternative has come along.
Java for example, resolves this by not having free functions at all. C3 solves it by not having static methods (nor
static variables). Consequently more functions becomes part of the module rather than the type.

**Q:** Why does only macros have ref arguments?
**Q:** Why do macros with trailing bodies require `;` at the end?

```c3
@test()
{
// code
}; // <- Why is this needed?
```

**A:** All macro calls, including those with a trailing body, are expressions, so it would be ambiguous
to let them terminate a statement without a much more complicated grammar. An example:

```c3
// How can the parser determine that the
// last `}` ends the expression? (And does it?)
int a = @test() {} + @test() {}
*b = 123;
// In comparison, the grammar for this is easy:
int a = @test() {} + @test() {};
*b = 123;
```

C3 strives for a simple grammar, and so the trade-off having to use `;` was a fairly
low prices to pay for this feature.

**Q:** Why do only macros have ref arguments?

**A:** Ref arguments break the general contract of a call: what looks like a pass-by-value
may suddenly be passed by reference. This makes code reading much harder, but is popular
Expand Down

0 comments on commit c6ed827

Please sign in to comment.