www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - _aaApply2

reply IchorDev <zxinsworld gmail.com> writes:
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
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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
prev sibling parent reply Quirin Schroll <qs.il.paperinik gmail.com> writes:
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
next sibling parent reply IchorDev <zxinsworld gmail.com> writes:
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
parent reply Nick Treleaven <nick geany.org> writes:
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:
 The issue is that iteration isn’t `nothrow`, but I don’t know 
 why. It makes no sense.
See https://issues.dlang.org/show_bug.cgi?id=21236.
 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
parent IchorDev <zxinsworld gmail.com> writes:
On Monday, 22 July 2024 at 16:32:38 UTC, Nick Treleaven wrote:
 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); } ```
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`.
Jul 25
prev sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
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:
 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.
[...] 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.
Jul 22
next sibling parent reply IchorDev <zxinsworld gmail.com> writes:
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
parent reply Nick Treleaven <nick geany.org> writes:
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
parent reply IchorDev <zxinsworld gmail.com> writes:
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
parent reply Nick Treleaven <nick geany.org> writes:
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:
 `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.
Where is that proposed? Is that really going to be safe?
Jul 25
parent IchorDev <zxinsworld gmail.com> writes:
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
prev sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
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:
 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.
[...] Sure it does.
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.
Jul 22