Jump to page: 1 2
Thread overview
July 21

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?

July 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

July 22

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.

July 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…

July 22
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.
July 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

July 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.

July 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:

>

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:

void main() @nogc
{
    int[string] aa;
    foreach (k, v; aa)
        assert(v);
}
July 22

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.

July 25

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.

« First   ‹ Prev
1 2