digitalmars.D - _aaApply2
- IchorDev (3/3) Jul 21 Shouldn't AAs be using a range interface instead of opApply these
- monkyyy (3/6) Jul 21 Probaly history, ranges were d2, aa's in d1 look basically the
- Quirin Schroll (5/8) Jul 22 The range interface doesn’t really support key–value pair
- IchorDev (10/16) Jul 22 Indeed, only one or the other. We should really fix this somehow
- Nick Treleaven (14/20) Jul 22 It's @safe for the compiler to generate a call to it if the loop
- IchorDev (4/13) Jul 25 I might’ve been thinking about how it couldn’t be used in `@nogc`
- H. S. Teoh (9/15) Jul 22 [...]
- IchorDev (6/9) Jul 22 The struct has one loop value instead of two though, so you have
- Nick Treleaven (3/8) Jul 22 `front` can't return a tuple, because the range has to store a
- IchorDev (2/4) Jul 25 This won’t be an issue anymore once we have `ref` fields.
- Nick Treleaven (2/6) Jul 25 Where is that proposed? Is that really going to be @safe?
- IchorDev (4/5) Jul 25 Oh shoot it’s only for local variables, my bad:
- Quirin Schroll (6/17) Jul 22 Not in the way it’s supposed to. `foreach (v; aa)` is supposed to
Shouldn't AAs be using a range interface instead of opApply these days? Is really there any reason for a `foreach` over an AA not to be `nothrow`?
Jul 21
On Sunday, 21 July 2024 at 16:44:56 UTC, IchorDev wrote:Shouldn't AAs be using a range interface instead of opApply these days? Is really there any reason for a `foreach` over an AA not to be `nothrow`?Probaly history, ranges were d2, aa's in d1 look basically the same https://digitalmars.com/d/1.0/hash-map.html
Jul 21
On Sunday, 21 July 2024 at 16:44:56 UTC, IchorDev wrote:Shouldn't AAs be using a range interface instead of opApply these days? Is really there any reason for a `foreach` over an AA not to be `nothrow`?The range interface doesn’t really support key–value pair iteration. An AA can be iterated by values or keys and values. A range interface can’t do that. The issue is that iteration isn’t `nothrow`, but I don’t know why. It makes no sense.
Jul 22
On Monday, 22 July 2024 at 11:11:53 UTC, Quirin Schroll wrote:The range interface doesn’t really support key–value pair iteration.Yes it does?An AA can be iterated by values or keys and values. A range interface can’t do that.Indeed, only one or the other. We should really fix this somehow though, because opApply sucks with all of D’s different function attributes.The issue is that iteration isn’t `nothrow`, but I don’t know why. It makes no sense.I guess because the function calls an un-marked delegate? I’m not even sure how I’m able to call it in ` safe` code though, and yet it supposedly uses the GC just for iteration? And heaven forbid C code tries to call it, since it’s `extern(C)` but supposedly throws…
Jul 22
On Monday, 22 July 2024 at 15:25:32 UTC, IchorDev wrote:On Monday, 22 July 2024 at 11:11:53 UTC, Quirin Schroll wrote:See https://issues.dlang.org/show_bug.cgi?id=21236.The issue is that iteration isn’t `nothrow`, but I don’t know why. It makes no sense.I guess because the function calls an un-marked delegate? I’m not even sure how I’m able to call it in ` safe` code though,It's safe for the compiler to generate a call to it if the loop body is safe. If the body wasn't safe, there would be an error anyway.and yet it supposedly uses the GC just for iteration?Do you have an example? The following compiles: ```d void main() nogc { int[string] aa; foreach (k, v; aa) assert(v); } ```
Jul 22
On Monday, 22 July 2024 at 16:32:38 UTC, Nick Treleaven wrote:I might’ve been thinking about how it couldn’t be used in ` nogc` code a year or two ago when we didn’t really have inferred ` nogc`.and yet it supposedly uses the GC just for iteration?Do you have an example? The following compiles: ```d void main() nogc{ int[string] aa; foreach (k, v; aa) assert(v); } ```
Jul 25
On Mon, Jul 22, 2024 at 11:11:53AM +0000, Quirin Schroll via Digitalmars-d wrote:On Sunday, 21 July 2024 at 16:44:56 UTC, IchorDev wrote:[...] Sure it does. Check the implementation in object.d, internally it uses a range over a struct that encapsulates the key and value pair. You can actually use this interface in user code too: aa.byKeyValue(). The non-range interface is really for compatibility with legacy code. T -- How many guacas are in 1 guacamole? 6.022*10^23, also known as Avocado's Number.Shouldn't AAs be using a range interface instead of opApply these days? Is really there any reason for a `foreach` over an AA not to be `nothrow`?The range interface doesn’t really support key–value pair iteration.
Jul 22
On Monday, 22 July 2024 at 15:28:45 UTC, H. S. Teoh wrote:You can actually use this interface in user code too: aa.byKeyValue(). The non-range interface is really for compatibility with legacy code.The struct has one loop value instead of two though, so you have to rewrite code to use `.key` and `.value`, which is not as good as being able to name the key & value yourself. It would’ve been possible for it to be implemented differently though: https://dlang.org/spec/statement.html#front-seq
Jul 22
On Monday, 22 July 2024 at 15:49:54 UTC, IchorDev wrote:The struct has one loop value instead of two though, so you have to rewrite code to use `.key` and `.value`, which is not as good as being able to name the key & value yourself. It would’ve been possible for it to be implemented differently though: https://dlang.org/spec/statement.html#front-seq`front` can't return a tuple, because the range has to store a pointer to a value so `front.value` can be mutated.
Jul 22
On Monday, 22 July 2024 at 16:00:05 UTC, Nick Treleaven wrote:`front` can't return a tuple, because the range has to store a pointer to a value so `front.value` can be mutated.This won’t be an issue anymore once we have `ref` fields.
Jul 25
On Thursday, 25 July 2024 at 07:46:33 UTC, IchorDev wrote:On Monday, 22 July 2024 at 16:00:05 UTC, Nick Treleaven wrote:Where is that proposed? Is that really going to be safe?`front` can't return a tuple, because the range has to store a pointer to a value so `front.value` can be mutated.This won’t be an issue anymore once we have `ref` fields.
Jul 25
On Thursday, 25 July 2024 at 15:30:54 UTC, Nick Treleaven wrote:Where is that proposed? Is that really going to be safe?Oh shoot it’s only for local variables, my bad: https://forum.dlang.org/thread/v11mh1$fvs$1 digitalmars.com Back on topic: how should we go about remedying this issue?
Jul 25
On Monday, 22 July 2024 at 15:28:45 UTC, H. S. Teoh wrote:On Mon, Jul 22, 2024 at 11:11:53AM +0000, Quirin Schroll via Digitalmars-d wrote:Not in the way it’s supposed to. `foreach (v; aa)` is supposed to run over values and `foreach (k, v; aa)` over keys `k` and values `v`. From the range interface, `front` can only return one thing. If it’s a tuple that can decay into two values, sure the `k, v` one works, but the `v` one gives you tuples, not values.On Sunday, 21 July 2024 at 16:44:56 UTC, IchorDev wrote:[...] Sure it does.Shouldn't AAs be using a range interface instead of opApply these days? Is really there any reason for a `foreach` over an AA not to be `nothrow`?The range interface doesn’t really support key–value pair iteration.
Jul 22