Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 31, 2014 How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
I am trying to foreach over a std.container.DList but it isn't working. I have tried the following code: https://gist.github.com/Binero/f30e56351baf05f1a2ec I am getting the following errors: /usr/include/dlang/dmd/std/container.d(1925): Error: template std.container.DList!ubyte.DList.insertBeforeNode cannot deduce function from argument types !()(typeof(null), int), candidates are: /usr/include/dlang/dmd/std/container.d(2096): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T)) /usr/include/dlang/dmd/std/container.d(2155): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isImplicitlyConvertible!(Stuff, T)) source/app.d(7): Error: template instance std.container.DList!ubyte.DList.insertBack!int error instantiating source/app.d(11): Error: invalid foreach aggregate list1 |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Mon, Mar 31, 2014 at 05:50:16PM +0000, Jeroen Bollen wrote: > I am trying to foreach over a std.container.DList but it isn't working. I have tried the following code: [...] Maybe try using opSlice: DList myList; foreach (e; myList[]) { ... } ? T -- Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Monday, 31 March 2014 at 18:24:39 UTC, H. S. Teoh wrote:
> On Mon, Mar 31, 2014 at 05:50:16PM +0000, Jeroen Bollen wrote:
>> I am trying to foreach over a std.container.DList but it isn't working. I
>> have tried the following code:
> [...]
>
> Maybe try using opSlice:
>
> DList myList;
> foreach (e; myList[]) { ... }
>
> ?
>
>
> T
Can you explain that syntax?
|
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Monday, 31 March 2014 at 19:26:23 UTC, Jeroen Bollen wrote: > On Monday, 31 March 2014 at 18:24:39 UTC, H. S. Teoh wrote: >> On Mon, Mar 31, 2014 at 05:50:16PM +0000, Jeroen Bollen wrote: >>> I am trying to foreach over a std.container.DList but it isn't working. I >>> have tried the following code: >> [...] >> >> Maybe try using opSlice: >> >> DList myList; >> foreach (e; myList[]) { ... } >> >> ? >> >> >> T > > Can you explain that syntax? nvm figured it out; Are constant DLists supposed to be un-foreach-able? Error: mutable method std.container.DList!(ubyte).DList.opSlice is not callable using a const object |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | Still not working: https://gist.github.com/Binero/f30e56351baf05f1a2ec /usr/include/dlang/dmd/std/container.d(1925): Error: template std.container.DList!ubyte.DList.insertBeforeNode cannot deduce function from argument types !()(typeof(null), int), candidates are: /usr/include/dlang/dmd/std/container.d(2096): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T)) /usr/include/dlang/dmd/std/container.d(2155): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isImplicitlyConvertible!(Stuff, T)) source/app.d(7): Error: template instance std.container.DList!ubyte.DList.insertBack!int error instantiating source/app.d(11): Error: need upper and lower bound to slice pointer |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Mon, Mar 31, 2014 at 07:30:11PM +0000, Jeroen Bollen wrote: > On Monday, 31 March 2014 at 19:26:23 UTC, Jeroen Bollen wrote: > >On Monday, 31 March 2014 at 18:24:39 UTC, H. S. Teoh wrote: > >>On Mon, Mar 31, 2014 at 05:50:16PM +0000, Jeroen Bollen wrote: > >>>I am trying to foreach over a std.container.DList but it isn't > >>>working. I > >>>have tried the following code: > >>[...] > >> > >>Maybe try using opSlice: > >> > >> DList myList; > >> foreach (e; myList[]) { ... } > >> > >>? > >> > >> > >>T > > > >Can you explain that syntax? > > nvm figured it out; > > Are constant DLists supposed to be un-foreach-able? > Error: mutable method std.container.DList!(ubyte).DList.opSlice is not > callable using a const object Argh, why is opSlice non-const? :-( Please file a bug. Not sure what you can do in the meantime... probably cast away const? Or avoid using const for containers if you can. The last time I tried that, I got stuck in a quagmire of partially-implemented const support with no way out of the mess, and I decided that non-const is less painful to work with. :-/ T -- English is useful because it is a mess. Since English is a mess, it maps well onto the problem space, which is also a mess, which we call reality. Similarly, Perl was designed to be a mess, though in the nicests of all possible ways. -- Larry Wall |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 03/31/2014 10:50 AM, Jeroen Bollen wrote: > I am trying to foreach over a std.container.DList but it isn't working. > I have tried the following code: > https://gist.github.com/Binero/f30e56351baf05f1a2ec > > I am getting the following errors: > > /usr/include/dlang/dmd/std/container.d(1925): Error: template > std.container.DList!ubyte.DList.insertBeforeNode cannot deduce function > from argument types !()(typeof(null), int), candidates are: > /usr/include/dlang/dmd/std/container.d(2096): > std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff > stuff) if (isInputRange!Stuff && > isImplicitlyConvertible!(ElementType!Stuff, T)) > /usr/include/dlang/dmd/std/container.d(2155): > std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff > stuff) if (isImplicitlyConvertible!(Stuff, T)) > source/app.d(7): Error: template instance > std.container.DList!ubyte.DList.insertBack!int error instantiating > source/app.d(11): Error: invalid foreach aggregate list1 Some notes: 1) DList is a struct. So, there is no need for 'new'. However, new is not an error but then the ~= syntax below cannot work on list1 without dereferencing. (You did not use ~=, I just liked it. :) ) (*list1) ~= cast(ubyte)3; // would work 2) For the same reason, list1[] becomes plain array slicing and the compiler is looking for "upper and lower bound to slice pointer". This would work: (*list1)[] 3) 1, 2, and 3 are ints. So, I had to cast them. import std.container; import std.stdio; void main() { auto list1 = DList!ubyte(); list1.insertBack(cast(ubyte)1); list1.insertBack(cast(ubyte)2); list1 ~= cast(ubyte)3; // alternative syntax foreach(ubyte item; list1[]) { writeln(item); } } Ali |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Monday, 31 March 2014 at 21:41:16 UTC, H. S. Teoh wrote: > Argh, why is opSlice non-const? :-( Please file a bug. If opSlice was const, then you'd get a const slice, with const reference. You wouldn't even be able to iterate on it. With some extra code, you could create some sort of "slice of immutable" type, but you'd still only be able to get const items. We *could* add a const *overload* but... > Or avoid using const for containers if you can. That. D's "turtles all the way down" const doesn't work like C++'s "head only" const. As a general rule, don't use too much const in D, *especially* for containers-like objects... At this point, adding a const overload would be nothing more that a still unusable mess, with extra maintenance overhead. |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 31-3-2014 19:50, Jeroen Bollen wrote: > I am trying to foreach over a std.container.DList but it isn't working. I have tried the following code: https://gist.github.com/Binero/f30e56351baf05f1a2ec > > I am getting the following errors: > > /usr/include/dlang/dmd/std/container.d(1925): Error: template std.container.DList!ubyte.DList.insertBeforeNode cannot deduce function from argument types !()(typeof(null), int), candidates are: > /usr/include/dlang/dmd/std/container.d(2096): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, T)) > /usr/include/dlang/dmd/std/container.d(2155): std.container.DList!ubyte.DList.insertBeforeNode(Stuff)(Node* n, Stuff stuff) if (isImplicitlyConvertible!(Stuff, T)) > source/app.d(7): Error: template instance std.container.DList!ubyte.DList.insertBack!int error instantiating > source/app.d(11): Error: invalid foreach aggregate list1 http://rosettacode.org/wiki/Strand_sort#D |
March 31, 2014 Re: How to foreach over a DList? | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Monday, 31 March 2014 at 21:55:03 UTC, monarch_dodra wrote: > If opSlice was const, then you'd get a const slice, with const reference. You wouldn't even be able to iterate on it. const opSlice does not mean it returns a const(Range). > With some extra code, you could create some sort of "slice of immutable" type, but you'd still only be able to get const items. Iterating over const items is the goal. > We *could* add a const *overload* but... > >> Or avoid using const for containers if you can. > > That. D's "turtles all the way down" const doesn't work like C++'s "head only" const. As a general rule, don't use too much const in D, *especially* for containers-like objects... > > At this point, adding a const overload would be nothing more that a still unusable mess, with extra maintenance overhead. Gotta disagree violently here. Asking for a const overload of opSlice isn't "too much const". std.container is just in a sorry state regarding const compatibility. That can and should be fixed. |
Copyright © 1999-2021 by the D Language Foundation