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

Zoadian no at no.no
Thu Jan 23 10:18:01 UTC 2020


On Thursday, 23 January 2020 at 05:06:21 UTC, Jonathan M Davis 
wrote:
> I do think that we should consider adding something like 
> pure(true) and pure(false) - and possibly something like 
> pure(default) or pure(inferred) - for attributes where that 
> makes sense, because it would work better with metaprogramming 
> (at least if an expression can be provided instead of only 
> boolean literals). And if we went with something like that, 
> arguably it doesn't make sense to add throw in addition to 
> nothrow, since throw could just be nothrow(false). However, the 
> double negative is arguably undesirable, and even if a boolean 
> solution would be desirable, this DIP is an improvement over 
> the current sitution and does not make it so that we can't add 
> a more flexible solution for negating function attributes in 
> general later.
>
> - Jonathan M Davis

I constantly need to turn on/off @safe and @nothrow based on 
version(WebAssembly).
so i'm all for adding a simple way to toggle attributes.
my proposal would be something like this:

```
struct pure {
private:
  enum P {
   Yes,No,Default,Inferred,
  }
  W _pure;
  this(P p) { _pure = p; }
public:
  this(bool b = true) { _pure = b ? P.Yes : P.No; }
  enum default = pure(P.Default);
  enum inferred = pure(P.Inferred);
}

alias impure(bool b = true) = pure!(!b);
```

so we can have:

```
@pure
@impure
@pure!false
@impure!false
@pure.default
@pure.inferred
@impure.default
@impure.inferred
```



More information about the Digitalmars-d mailing list