@safe(bool)

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Sat Aug 19 18:05:39 PDT 2017


On Sunday, 20 August 2017 at 00:49:28 UTC, Nicholas Wilson wrote:
>
> [...]
>
> With DIP 1012 you should be able to go
>
>  struct Container(T, bool safetyOn = true)
>  {
>  	static if(safe)
>  		RefCounted!(T[]) data;
>  	else
>  		T[] data;
>
>  	auto opSlice() @safeIf!safetyOn {
>  		return Range(data, 0, data.length);
>  	}
>  }
>
>  template safeIf(bool cond)
>  {
>      static if (cond) alias safeIf = AliasSeq!(safe);
>      else                   alias safeIf = AliasSeq!();
>  }
>
> or even just
>
>  struct Container(T, FunctionSafety safetyOn = safe)
>  {
>  	static if(safe)
>  		RefCounted!(T[]) data;
>  	else
>  		T[] data;
>
>  	auto opSlice() @safetyOn {
>  		return Range(data, 0, data.length);
>  	}
>  }
>
> Container!int foo; // Container!(int, safe)
> Container!(int, system) bar;

This is indeed, a nice solution. I am a _bit_ worried about 
abuse, and loss of modularity, but aside from that, I think it's 
a better solution overall.

> The only downside is that the second form leaves itself open to

Easily fixed with a template constraint, right?


This could potentially render a large portion of the projects on 
code.dlang.org broken though. What would be nice, is if 
code.dlang.org regularly built all the projects, and notified the 
authors of the breakage, possibly sending a list of recent 
compiler changes as well.



More information about the Digitalmars-d mailing list