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

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Aug 17 03:12:36 UTC 2018


On Tuesday, 7 August 2018 at 08:20:55 UTC, Basile B. wrote:
> 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.

I have decided to make the DIP be consistent with `in` contracts: 
that is allow both an expression form and block statement form. 
The block state meant can use static foreach to static assert on 
variadic lists, while the expression from is more concise.


More information about the Digitalmars-d mailing list