Bug 24633 – Document opApply as an alias to a function template instance

Status
NEW
Severity
enhancement
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-06-26T16:29:41Z
Last change time
2024-12-15T15:28:22Z
Keywords
pull, spec
Assigned to
No Owner
Creator
Bolpat
Moved to GitHub: dlang.org#4142 →

Comments

Comment #0 by qs.il.paperinik — 2024-06-26T16:29:41Z
When `opApply` is an alias to an explicit function template instance, the explicit instantiation is used for overload resolution against potentially many `opApply` overloads and it is used to infer the types of `foreach` variables, however, the delegate created by the compiler in the `foreach` body lowering is passed to the function template uninstantiated. By this mechanism, when overloading `opApply`s based on the number of intended `foreach` variables, type inference is possible **and** function attributes (e.g. `@safe`) will be inferred. Example: ```d struct A { int opApply(scope int delegate(long) body) => body(42); } struct B { int opApply(Body)(scope Body body) => body(42); } struct C { int opApplyImpl(Body)(scope Body body) => body(42); alias opApply = opApplyImpl!(int delegate(long)); } void main() @nogc nothrow pure @safe { // Error: `@nogc` function cannot call non-@nogc function `A.opApply` // Error: `pure` function cannot call impure function `A.opApply` // Error: `@safe` function cannot call `@system` function `A.opApply` // Error: function `A.opApply` is not `nothrow` foreach (x; A()) { } // Error: cannot infer type for `foreach` variable `x` foreach (x; B()) { } // Good: foreach (x; C()) { static assert(is(typeof(x) == long)); assert(x == 42); } } ``` This behavior is great and should be specified as intended.
Comment #1 by dlang-bot — 2024-06-26T16:49:30Z
@Bolpat created dlang/dlang.org pull request #3859 "Specify `opApply` as an alias to a function template instance" fixing this issue: - Fix Bugzilla Issues 23666, 17953, 23116, and 24633 https://github.com/dlang/dlang.org/pull/3859
Comment #2 by robert.schadek — 2024-12-15T15:28:22Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dlang.org/issues/4142 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB