How can I tell D that function args are @nogc etc.

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 11 15:00:49 UTC 2024


On Thursday, 11 April 2024 at 03:17:36 UTC, John Dougan wrote:
> Interesting. Thank you to both of you.
>
> On Wednesday, 10 April 2024 at 17:38:21 UTC, Steven 
> Schveighoffer wrote:
>> On Wednesday, 10 April 2024 at 11:34:06 UTC, Richard (Rikki) 
>> Andrew Cattermole wrote:
>>> Place your attributes on the right hand side of the function, 
>>> not the left side.
>>>
>>> Use the left side for attributes/type qualifiers that go on 
>>> the return type.
>>
>> Just a word of warning, this explanation suggests putting 
>> qualifiers on the left side would affect the return type, this 
>> is not the case.
>
> So in my example, what did I actually tell the compiler with 
> the placement of the attributes? And how was it different 
> between the function type alias declaration, and the actual 
> function declaration?
>
> More specifically, what are the semantic differences below?
> ```d
> alias FnPrefixT = @nogc nothrow @safe bool function(int);
> // Versus
> alias FnSuffixT = bool function(int) @nogc nothrow @safe;
> ```

So D can provide a nice mechanism to show what is happening -- 
`pragma(msg, ...)`

If I do that with the two types above I see something *very* 
interesting:

```d
pragma(msg, FnPrefixT);
pragma(msg, FnSuffixT);
```

```
bool function(int) nothrow @nogc
bool function(int) nothrow @nogc @safe
```

That surprises me. `nothrow` and `@nogc` go onto the type, but 
not `@safe` if put before the declaration? I have no idea why. 
All I can think of is that it is a bug.


> and
> ```d
> @nogc nothrow @safe bool fnPrefix(int) { stuff }
> // Versus
> bool fnSuffix(int) @nogc nothrow @safe  { stuff }
> ```

```d
pragma(msg, typeof(fnPrefix));
pragma(msg, typeof(fnSuffix));
```

```
nothrow @nogc @safe bool(int)
nothrow @nogc @safe bool(int)
```

(as expected)

-Steve


More information about the Digitalmars-d-learn mailing list