Why is this not allowed?

Steven Schveighoffer schveiguy at gmail.com
Mon Oct 7 18:06:30 UTC 2024


On Sunday, 6 October 2024 at 05:41:10 UTC, ryuukk_ wrote:
> There is no new syntax to invent
>
> instead of writing this error:
> ```
> onlineapp.d(8): Error: no identifier for declarator `stats`
> ```
>
> you generate a random identifier and assign it
>
> ```D
>     extern(D) static Identifier 
> generateAnonymousId(const(char)[] name)
> ```
>
> https://github.com/dlang/dmd/blob/master/compiler/src/dmd/identifier.d#L165
>
> I'm very far from being bright, but i know simple logic

The problem is not the ability to generate an id, the problem is 
that the declaration is over. Anything after the closing brace is 
a *new declaration*.

Whitespace is not significant in D, so you have to invent a new 
syntax to to mean the declaration continues.

D sees this:

```d
struct {
   int x;
   int y;
} something;
```

as this:

```d
struct {
   int x;
   int y;
}
// DECLARATION OF ANONYMOUS STRUCT OVER.

something;  // this is something different, unrelated to the 
struct above.
```

One declaration defines an anonymous struct, and one that is a 
new declaration `something;`. Or if you are in a function, 
`something;` is treated as a statement.

You can't change existing syntax, as that would break code, so 
you need new syntax.

C allows this because structs are *required* to end with a 
semicolon. So the compiler knows the thing after the brace is 
part of the same declaration.

-Steve


More information about the Digitalmars-d-learn mailing list