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

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 10 17:38:21 UTC 2024


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.

Attributes apply to the *declaration*. In some cases this 
effectively applies to the return type, in some cases it applies 
to the function, in some cases it applies to the context pointer.

In order to apply type constructors to the return type, you need 
to use parentheses:

```d
const int  foo(); // const applies to the context pointer of 
`foo`, not `int`
const(int) bar(); // const applies to `int` return type
ref int baz(); // ref applies to `baz`, which in turn means "ref 
returning function"
```

Where this becomes tricky is return types that are function 
pointers/delegates. Then using the right side of the 
function/delegate *type* is the only way.

```d
@safe void function() foo(); // `foo` is safe, the function 
pointer it returns is not
void function() @safe bar(); // `bar` is not safe, the function 
pointer returned is
void function() @safe baz() @safe; // both are safe
```

-Steve


More information about the Digitalmars-d-learn mailing list