DIP64

From D Wiki
Jump to: navigation, search
Title: DIP64 Attribute Cleanup
DIP: 64
Version: 1
Status: Draft
Created: 2014-06-20
Last Modified: 2014-06-20
Author: Brian Schott
Links: Newsgroup Discussion

Abstract

The number of annotations in the D language continues to grow. Adding all of the correct annotations to declarations can cause these declarations to become very verbose.

There is some inconsistency between the syntax of various attributes because of historical reasons. For example, "pure" and "@nogc" are inconsistent because one of them uses the @identifier syntax and the other does not, yet they are both compiler-defined attributes.

This DIP intends to fix these issues in two ways:

  1. The definition of attribute sets, which allow users to combine multiple annotations into a single annotation and
  2. The deprecation of the old-style attributes that do not use the @-syntax. A tool will be created that can easily update code to use the new attributes. (e.g. by translating "nothrow" to "@nothrow")

Atribute Sets

First, an example. The following code using attribute sets:

@spiffy = @pure @nothrow @safe;

float mul(float a, float b) @spiffy
{
    return a * b;
}

is semantically equivalent to the following code that does not:

float mul(float a, float b) @pure @nothrow @safe
{
    return a * b;
}

Arguments to attribute sets will be passed on to the attributes that compose the set. For example:

@special(name) = @serializable(name) @entity(name);
@special("abc")
struct SpecialStruct { int a; }

is the same as

@serializable("abc") @entity("abc")
struct SpecialStruct { int a; }

Grammar Changes

A new rule called AttributeSetDeclaration will be added to the Declaration rule:

Declaration:
	...
	AttributeSetDeclaration
	...

AttributeSetDeclaration will be defined as follows:

AttributeSetDeclaration:
    Property '=' Property+ ';'

Attribute Consistency

Keywords that are only attributes (i.e. they are not also storage classes or type constructors) will be deprecated. The compiler front-ends will need to be modified to recognize the new @-versions of the attributes as equivalent to their old non-@-versions (e.g. "@pure" is equivalent to "pure").

To aid in this transition a tool will be constructed on top of the lexer contained in the D-Scanner project. Because this is only a keyword substitution the chances of this tool failing are incredibly low. All whitespace, comments, and other formatting will be preserved by this tool.

Copyright

This document has been placed in the Public Domain.