static foreach, expression-Based Contract Syntax and better error messages

Basile B. b2.temp at gmx.com
Tue Aug 7 08:20:55 UTC 2018


On Tuesday, 7 August 2018 at 07:33:49 UTC, Nicholas Wilson wrote:
> so the following
> void foo(As...)(As as)
> in
> {
>    static foreach (a ;as)
>    assert(a>0);
> }
> do
> {
> }
>
> void main()
> {
>      foo(1,2,3,4,5);
> }
>
> passes and compiles, whereas
>
> void foo(As...)(As as)
> static foreach (a ;as)
>     in(a>0)
> {
> }
>
> void main()
> {
>      foo(1,2,3,4,5);
> }
>
> does not compile.  I suppose thats fair enough, there is after 
> all a not very verbose workaround.
>
> I only note this because in researching examples for my 
> multiple template constraints DIP[1], I noticed while I can 
> make individual clauses easier to understand, I realised that 
> recursive templates clauses are still going to be horrible so 
> it would be useful to be able to do
>
> void foo(As...)(As as)
> static foreach (alias A ;As)
>     if(isFoo!A)
> {
>     //...
> }
> [...]
> Should this work? And is this something people would like to 
> see added to the DIP?

No. At this point syntax of contracts or constraints is getting 
*really* mad.
I think it's better to put the static loop in the function body, 
especially since with assert you can format a nice message, while 
not with constraints.




More information about the Digitalmars-d mailing list