[Issue 14196] opApply and nothrow don't play along

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 29 01:35:29 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=14196

--- Comment #2 from Bolpat <qs.il.paperinik at gmail.com> ---
You can overload on attributes:

struct MyArray(T)
{
    int opApply(scope int delegate(ref T)         dg);
    int opApply(scope int delegate(ref T) nothrow dg) nothrow;
}

This is not DRY and if the code inside opApply might throw depending on T, the
second overload won't compile.
Using templates breaks generic code that omits the foreach variables' types
(which is what generic code usually does).

If your case is only about nothrow (or any other single attribute), you can
define a template 
    int opApplyImpl(DG)(scope DG dg)
and alias opApply to specific instantiations:
    alias opApply = opApplyImpl!(int delegate(ref T)        );
    alias opApply = opApplyImpl!(int delegate(ref T) nothrow);
Whether this solution is viable or not depends on details of your case.

--


More information about the Digitalmars-d-bugs mailing list