Cannot Qualify Variadic Functions with Lazy Arguments as nothrow

Maxim Fomin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 14 03:18:10 PDT 2015


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);
}


More information about the Digitalmars-d-learn mailing list