Difference between revisions of "DIP79"

From D Wiki
Jump to: navigation, search
(possible solutions in progress)
m (Possible solution)
Line 86: Line 86:
 
== Possible solution ==
 
== Possible solution ==
  
There has been many NG discussions about possible syntax changes which will add negation of attributes possible ([[#Alternative solutions | see alternative solutions section]]).
+
There has been many NG discussions about possible syntax changes which will add negation of attributes possible ([[#Alternative solutions|see alternative solutions section]]).
 
<p>In this DIP I will address solution which use exclamation mark (<strong><code>!</code></strong>) for attribute negation.</p>
 
<p>In this DIP I will address solution which use exclamation mark (<strong><code>!</code></strong>) for attribute negation.</p>
 
<p>Previous code example with this DIP could look like this:</p>
 
<p>Previous code example with this DIP could look like this:</p>

Revision as of 14:21, 28 May 2015

Title: Negation of attributes
DIP: 79
Version: 1
Status: Draft (in progress)
Created: 2015-05-28
Last Modified: 2015-05-28
Author: Daniel Kozák
Links: [1]

Abstract

Proposal for a syntax change which will allow negation of attributes like (final, pure, nothrow, @nogc) without the need for new keywords.

Rationale

In D classes all non-final and non-template methods are virtual by default.

class C
    /* virtual */ void someMethod(){}
    /* virtual */ void someOtherMethod(){}
    ...
}

This in theory reduce some kind of bugs, when one forget to mark method as virtual. On the other hand it leads to some speed penalty. To improve speed you could mark your methods as final like this:

class C
{
    final void someMethod(){}
    final void someOtherMethod(){}
    ...
}

This works well, but is somehow annoying and tiresome to mark all methods as final. So some patterns has been introduced:

class C
{
// make all methods final
final:
    /* final */ void someMethod(){}
    /* final */ void someOtherMethod(){}
    ...
}

This is more handy and leads to less typing, but it has some drawbacks. If you need add few methods which are virtual or variables, you are forced to put them before final:.

class C
{
// all virtual methods must be here
    void someVirtualMethod(){}
    void nextVirtualMethod(){}

// even all variables must be here 
    int someVar;

// make all methods final
final:
    /* final */ void someMethod(){}
    /* final */ void someOtherMethod(){}
    ...
}

This is too limiting. Negation of attributes will remove these limits.

Possible solution

There has been many NG discussions about possible syntax changes which will add negation of attributes possible (see alternative solutions section).

In this DIP I will address solution which use exclamation mark (!) for attribute negation.

Previous code example with this DIP could look like this:

class C
{
final:
    /* final */ void someMethod(){}
    !final void someVirtualBetweenFinalMethod(){}
    /* final */ void someOtherMethod(){}
    ...
!final:
    void someVirtualMethod(){}
    void nextVirtualMethod(){}

    int someVar;
}

Alternative solutions

TODO

Copyright

This document has been placed in the Public Domain.