`this` as function attribute

Loara loara at noreply.com
Sat Jun 4 16:57:07 UTC 2022


In D attributes that can be associated to functions can be 
divided into two groups:

- attributes referring the function itself;
- attributes referring the implicit `this` function parameter,

so attributes like `pure`, `@safe`, `ref`, ... belong to the 
first group and `const`, `shared`, `scope` to the second one.

Maybe in the future would be necessary to apply the same 
attribute to the member function or the `this` attribute (or 
both) that forces you to reserve an additional attribute name in 
order to distinguish them.

Another approach is to use the `this` keyword in order to specify 
that the passed attributes refers to the implicit `this` 
parameter instead of the function member:

```d
class MyClass{
...
void myfun() @A @B this( @A @C ) {
...
}
...
}
```
In this example function `myfun` has the attributes `@A`, `@B` 
whereas the `this` parameter has only `@A`, `@C` attributes.

In order to avoid backward incompatibility the `this` syntax 
won't be necessary for all the attributes that can be associated 
to function parameters but don't have sense for pure functions 
like `const`, `immutable`...


More information about the Digitalmars-d mailing list