Difference between revisions of "DIP79"

From D Wiki
Jump to: navigation, search
m (Possible solution)
(Alternative solutions)
Line 107: Line 107:
  
 
== Alternative solutions ==
 
== Alternative solutions ==
TODO
+
=== ~attribute ===
 +
<p>
 +
This is same as proposed solution. But instead of <strong><code>!</code></strong> char, <strong><code>~</code></strong> char will be used.
 +
</p>
 +
<p>
 +
I like this one almost same as proposed solution. It shares same pros a cons.
 +
</p>
 +
=== @disable(attribute) ===
 +
<p>
 +
This is almost same as two previos solution. But instead of <strong><code>~</code></strong>, <strong><code>@disable(attribute)</code></strong> expresion is used.
 +
</p>
 +
<p>
 +
This one is little more verbose and need some extra step in parsing phase, so I would still prefer one of previous solutions.
 +
</p>
 +
=== attribute(bool expression) ===
 +
<p>
 +
This one is quite different. In basic variants <code>attribute(true) and attribute(false)</code> is same as all solutions above with just another syntax. But this syntax will allow us to use conditional expresion to affect result attribute.
 +
Eg.:
 +
</p>
 +
<syntaxhighlight lang="d">
 +
version(MakeItVirtual)
 +
    enum virtual = true; // someVirtualOrFinalMethod will be virtual
 +
else
 +
    enum virtual = false; // someVirtualOrFinalMethod will be final
 +
 
 +
class C
 +
{
 +
final:
 +
    final(virtual) someVirtualOrFinalMethod (){}
 +
}
 +
</syntaxhighlight>
 +
 
 +
<p>
 +
At first glance I like this solution most, because it is more powerful. But than I realize it makes code readability worse. Another disadvantage, It is not so easy to implement.
 +
</p>
  
 
== Copyright ==
 
== Copyright ==

Revision as of 15:04, 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

~attribute

This is same as proposed solution. But instead of ! char, ~ char will be used.

I like this one almost same as proposed solution. It shares same pros a cons.

@disable(attribute)

This is almost same as two previos solution. But instead of ~, @disable(attribute) expresion is used.

This one is little more verbose and need some extra step in parsing phase, so I would still prefer one of previous solutions.

attribute(bool expression)

This one is quite different. In basic variants attribute(true) and attribute(false) is same as all solutions above with just another syntax. But this syntax will allow us to use conditional expresion to affect result attribute. Eg.:

version(MakeItVirtual)
    enum virtual = true; // someVirtualOrFinalMethod will be virtual
else
    enum virtual = false; // someVirtualOrFinalMethod will be final

class C
{
final:
    final(virtual) someVirtualOrFinalMethod (){}
}

At first glance I like this solution most, because it is more powerful. But than I realize it makes code readability worse. Another disadvantage, It is not so easy to implement.

Copyright

This document has been placed in the Public Domain.