Rhyme and reason for function annotations?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Nov 4 03:40:21 UTC 2024


On Sunday, November 3, 2024 5:01:20 PM MST Andy Valencia via Digitalmars-d-
learn wrote:
> A function can be described as, say, private, or pure, or @nogc.
> When does an annotation have an '@'?  Also, a function can be
> annotated
>
> int myfunc(char *arg) pure {
> }
>
> Although I find:
>
> pure int myfunc(char *arg) {
> }
>
> Also works.  So what annotations have @'s, and when do they go
> with the function declaration, and when do they go after the
> argument declaration?
>
> I don't need an exhaustive list, I'm much more interested in the
> underlying philosophy which assigns what to where.

There really isn't one. Function attributes originally didn't have @ on
them, but newer ones got an @ to avoid adding new keywords.

And in general, you can put function attributes on either side of a
function, and it doesn't matter. It's a matter of preference. The main
gotcha there is that if an attribute could affect either the function or the
return type, then it's going to affect the function if you don't use parens,
e.g.

struct S
{
    const int foo() { ... }
}

is going to make foo a const member function even though it looks like
you're saying that you want const int. To get that, you need

    const(int) foo() { ... }

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list