opApply and attributes

Stanislav Blinov stanislav.blinov at gmail.com
Tue Jul 7 13:52:49 UTC 2020


On Tuesday, 7 July 2020 at 13:33:41 UTC, Paul Backus wrote:

> You can make opApply a template:
>
>     int opApply(Dg)(Dg dg)
>         if (is(Dg : scope int delegate(ref E)))
>     {
>         // etc.
>     }
>
> Because `scope int delegate(ref E) @safe` implicitly converts 
> to `scope int delegate(ref E)`, this version will accept both 
> @safe and non- at safe delegates. (And likewise for the other 
> function attributes.)

Yeah, but unfortunately then this won't work:

> 	foreach(elem; test) {
> 		assert(elem == key);
> 	}

you'd have to spell out the types for `elem`.


More information about the Digitalmars-d-learn mailing list