Cannot Qualify Variadic Functions with Lazy Arguments as nothrow

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 14 11:00:22 PDT 2015


On Thursday, 14 May 2015 at 10:18:13 UTC, Maxim Fomin wrote:
> On Thursday, 14 May 2015 at 09:53:20 UTC, Per Nordlöw wrote:
>> At
>>
>> https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L43
>>
>> I've implemented a function either() with behaviour similar to 
>> the `or` function/operator in dynamic languages such as Python 
>> and Lisp.
>>
>> I'm almost satisified with it except that the lazy evaluation 
>> at
>>
>> https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L45
>>
>> cannot be made nothrow.
>>
>> If I qualify the function as nothrow DMD complains as
>>
>> algorithm_ex.d(45,16): Error: 'a' is not nothrow
>> algorithm_ex.d(46,29): Error: '_param_1' is not nothrow
>>
>> I don't see a reason why any of these two cases should throw.
>
> Lazy argument is essentially delegate/function. Currently there 
> is no
> way to mark it as nothrow.
>
>> The same problem occurs if I make the implementation use only 
>> one function and check the recursion termination case with 
>> `static if (bs.length == 1)` instead.
>>
>> Is there a workaround for this?
>
> One way to address is to use delegate explicitly.
>
> int foo(lazy int a) //nothrow
> {
> 	return a;
> }
>
> int bar(int delegate() nothrow dg) nothrow
> {
> 	return dg();
> }
>
> void main() nothrow
> {
> 	int a;
> 	bar(()=>a);
> }

That does not feel right at all. D's filosophy is to infer these 
things.Are you saying that I should create to overloads for the 
leaf case of either(), namely one that takes a nothrow delegate 
as argument and another overload that handles the throw case. 
Further you example functions are not templates. either() must be 
a template. Could you please show how to modify either() to use 
your delegate-version instead.


More information about the Digitalmars-d-learn mailing list