DIP 1029---Add throw as Function Attribute---Community Review Round 1

Arine arine123445128843 at gmail.com
Wed Jan 15 15:26:02 UTC 2020


On Wednesday, 15 January 2020 at 07:33:14 UTC, Walter Bright 
wrote:
> On 1/14/2020 3:04 PM, Adam D. Ruppe wrote:
>> to make `nothrow:` (etc) go into the child scopes except 
>> templates means we have a less disruptive way to make this 
>> work without formally changing the defaults.
>
> As I remarked before, this will have the unintended consequence 
> of causing the "best practice" to be listing all the attributes 
> at the opening brace of all struct declarations just in case 
> someone 5000 lines previously added an:
>
>     attribute:
>
> Best practice:
>
>     struct S
>     {
>         throw @safe pure @nogc etc. etc. etc. etc. ...
>         ...
>     }
>
> and then have to remember to add to that list for every new 
> attribute. It's ugly, and people are going to hate it.

I already have `@nogc nothrow pure:` at the top of almost every 
module. I also have the same at the top of every struct. It's 
happening right now, and it's not optional because you have to 
set the attributes based on the default. The only reason you 
never would have come across this is if you don't care about 
using those attributes as the default.


*Current Day Best Practice, That's unavoidable if you want these 
attributes for everything*:

     module blah;
     @nogc nothrow pure:

     struct A {
     @nogc nothrow pure:
     }

     struct B {
     @nogc nothrow pure:
     }


     struct C {
     @nogc nothrow pure:
         void foo() {
             static struct D {
             @nogc nothrow pure:
             }
         }
     }


>     struct S
>     {
>         throw ****@safe**** pure @nogc etc. etc. etc. etc. ...
>         ...
>     }

@safe already passes it's value onto a struct's scope. Yet the 
problem you are describing doesn't exist for it.


     @safe:

     struct S {
         void foo() {
             int* a = cast(int*)0; // error not allowed in @safe
         }
     }

That problem **does** exist now, because of the very thing you 
are saying prevents it. Because I can't just do `@nogc nothrow 
pure:` at the top of the module, I have to specify every single 
scope to be `@nogc nothrow pure:`. The language literally forces 
you to do it, unless you want to label each and every function 
with those attributes. I'm completely blown away, you don't even 
know that @safe passes through to the next scope? Or did you just 
add that attribute to exaggerate the problem?

The more I see this problem, and just how little Walter seems to 
understand of it. Being able to set defaults at the module level 
seems to make the most sense to me. Leave the garbage 
`attribute:` behavior the way it is, even if it is garbage. And 
add it so you can set the default based on the module.

     @safe module blah; // defaults to safe
     @system nothrow module blah2; // default is @system and 
nothrow

This breaks the least amount of code, and if you want to set 
@safe to be the default. All someone has to do to get their code 
to work again is add `@system` in front of the module.

     nothrow module blah;

     throw: // same awful behavior with scope

     void foo(); // "throw"

     struct A {
         void bur(); // nothrow
     }

Whatever I guess, this has just gone from one bad DIP to another 
to another to another since Andrei left.


More information about the Digitalmars-d mailing list