Bug 14196 – opApply and nothrow don't play along

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-02-17T19:44:02Z
Last change time
2024-12-13T18:40:36Z
Assigned to
No Owner
Creator
Mathias LANG
Moved to GitHub: dmd#17698 →

Comments

Comment #0 by pro.mathias.lang — 2015-02-17T19:44:02Z
It's impossible to have both opApply with type inference and compiler-checked nothrow for a user-defined type. Say you've got a container of T, and you want to implement opApply. Fine, the signature is, as you know (I only covert the first one, but it applies to both): int opApply(int delegate(ref T) del); But the issue is, you can't pass it a nothrow delegate, so you'll have to make it a template: int opApply(DG)(DG del); It works, but you loose type deduction, e.g for is(T == string): foreach (ref elem; arr) // Doesn't compile anymore foreach (ref string elem; arr) // So you'll have to use this. Full example: struct MyArray(T) { T[] data; int opApply(DG)(DG del) { for (size_t idx; idx < data.length; ++idx) if (auto r = del(data[idx])) return r; return 0; } } void neverThrow(string s) nothrow {} void mightThrow(string s) {} void testNothrow(T)(ref T arr) nothrow { foreach (ref elem; arr) neverThrow(elem); } void testThrow(T)(ref T arr) { foreach (ref elem; arr) mightThrow(elem); } void main() { // Note that type deduction doesn't work here. auto arr = MyArray!string(["1", "2", "3", "4", "5", "42"]); testNothrow(arr); testThrow(arr); }
Comment #1 by ilyayaroshenko — 2015-08-06T19:09:39Z
Comment #2 by qs.il.paperinik — 2020-12-29T01:35:29Z
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.
Comment #3 by pro.mathias.lang — 2020-12-29T10:57:22Z
For reference, I mentioned a solution to this problem which I'm working on during DConf: https://www.youtube.com/watch?v=9lOtOtiwXY4
Comment #4 by robert.schadek — 2024-12-13T18:40:36Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17698 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB