DIP 1038: @nodiscard - Unofficial "post-final" review

Jacob Carlborg doob at me.com
Fri Feb 26 20:00:52 UTC 2021

On 2021-02-23 00:32, Paul Backus wrote:

> @nodiscard is a user-defined attribute, and the compiler currently 
> allows multiple declarations of a function that differ only by UDAs. [1] 
> Again, I agree that this should probably be an error, but since it is an 
> issue that affects UDAs in general, it should be addressed in a Bugzilla 
> issue, not DIP 1038.

I would like to point out that compiler recognized user defined 
attributes were introduced as an alternative to keywords and attributes 
prefixed with `@`. Keywords are reserved words and cannot be used for 
user defined symbols. Attributes prefixed with `@`, i.e. `@property`, 
were introduced as a form of an additonal namespace for keywords. The 
word after `@` is not reserved so it can be used as user defined 
symbols. After that, user defined attributes were introduced. Even 
though you can have a user defined symbol named `property`, you cannot 
use that as a UDA, because it would cause a conflict with `@property`. 
This makes attributes prefixed with `@` for or less the same as keywords.

Then compiler recognized UDAs were introduced to solve all of this. 
`@selector` is a recognized UDA, but you can still name your symbols 
`selector` and you can still use that as a UDA. If there's a conflict 
with the regular UDA and the compiler recognized UDA, the user can use 
the normal language features to disambiguate the names, i.e. renamed 
imports, static imports, aliases an so on.

That means that the compiler should be free to, more or less, add any 
semantic behavior it wants to a compiler recognized UDA. For example, 
both the `@selector` and the `@optional` compiler recognized UDAs have 
several restrictions and other semantic behavior which a regular UDA 
doesn't have. They can only appear once in a method declaration, they 
can only be attached to methods with Objective-C linkage, they cannot be 
attached to templates, the number of colons in the string passed to 
`@selector` need to match the number of parameters. `@optional` affects 
how a class implements an interface.

If you say that you cannot add a specific semantic behavior to a 
compiler recognized UDA because it's a UDA it all falls apart. If you 
don't add any special semantic behavior at **all**, it would just be a 
regular UDA.

/Jacob Carlborg

More information about the Digitalmars-d mailing list