Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 05, 2005 Overload In | ||||
---|---|---|---|---|
| ||||
This seems like a stupid question, and sorry if it's been covered before. Is there any way to overload the "in" operator, or is it a construct created exclusively for D's built-in associative arrays? If the latter, can it be changed? John Demme |
June 05, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Demme | John Demme wrote:
> This seems like a stupid question, and sorry if it's been covered
> before. Is there any way to overload the "in" operator, or is it a
> construct created exclusively for D's built-in associative arrays?
I thought it was overloadable, but checking the documentation I couldn't find any mention of it. Did this not happen? I'd like to have it, myself.
-- Chris Sauls
|
June 07, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote:
> John Demme wrote:
>
>> This seems like a stupid question, and sorry if it's been covered
>> before. Is there any way to overload the "in" operator, or is it a
>> construct created exclusively for D's built-in associative arrays?
>
>
> I thought it was overloadable, but checking the documentation I couldn't find any mention of it. Did this not happen? I'd like to have it, myself.
>
> -- Chris Sauls
hmm, there's an opApply for use with "foreach".
I absolutly don't know what it does, but it may have something to do with "in", just a thought.
|
June 07, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | Hasan Aljudy wrote:
> hmm, there's an opApply for use with "foreach".
> I absolutly don't know what it does, but it may have something to do with "in", just a thought.
The opApply overload is for making classes/structs foreach-able. It takes a delegate wrapping the body of a foreach statement and passes values to it. No connection to the 'in' operator at all. (Although it is one of my favorite D-isms.)
-- Chris Sauls
|
June 08, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote:
> Hasan Aljudy wrote:
>
>> hmm, there's an opApply for use with "foreach".
>> I absolutly don't know what it does, but it may have something to do with "in", just a thought.
>
>
> The opApply overload is for making classes/structs foreach-able. It takes a delegate wrapping the body of a foreach statement and passes values to it. No connection to the 'in' operator at all. (Although it is one of my favorite D-isms.)
>
> -- Chris Sauls
Well, I just thought that, you know .. "foreach" means "foreach x in y", and opApply would be in "y", and it would take "x" as a parameter .. so it makes sense to think of it as the overload for "in".
Oh wait, I think I'm messing up ..
would someone care to explain to me how "foreach" exactly works, and how does it go with the "opApply"?
I don't really understand the reference pages for it, maybe because I never worked with a "foreach" kind of statement.
|
June 08, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hasan Aljudy | On Tue, 07 Jun 2005 18:06:26 -0600, Hasan Aljudy wrote: [snip] > Oh wait, I think I'm messing up .. > would someone care to explain to me how "foreach" exactly works, and how > does it go with the "opApply"? > I don't really understand the reference pages for it, maybe because I > never worked with a "foreach" kind of statement. Here is a longer example code that might help.. <code> //---------------------------- import std.stdio; import std.random; class Foo { uint array[]; typedef uint UnsortedFoo; typedef uint ReverseFoo; typedef uint RandomFoo; // Return the data as a sorted list. int opApply(int delegate(inout int i, inout uint) dg) { int result = 0; uint[] sa; sa = array.dup.sort; for (int i = 0; i < sa.length; i++) { result = dg(i, sa[i]); if (result) break; } return result; } // Return the data as a reverse sorted list. int opApply(int delegate(inout int i, inout ReverseFoo) dg) { int result = 0; uint[] sa; sa = array.dup.sort; for (int i = sa.length-1; i >= 0; i--) { int j; j = sa.length - i - 1; result = dg(j, cast(ReverseFoo)sa[i]); if (result) break; } return result; } // Return the data as an unsorted list. int opApply(int delegate(inout int i, inout UnsortedFoo) dg) { int result = 0; for (int i = 0; i < array.length; i++) { result = dg(i, cast(UnsortedFoo)array[i]); if (result) break; } return result; } // Return the data in random order int opApply(int delegate(inout int i, inout RandomFoo) dg) { int result = 0; bool[] Order; int i; Order.length = array.length; while (i < array.length) { int q; q = (std.random.rand() % array.length); if (Order[q] == false) { result = dg(i, cast(RandomFoo)array[q]); if (result) break; Order[q] = true; i++; } } return result; } } void main() { Foo a = new Foo(); a.array ~= 73; a.array ~= 82; a.array ~= 11; a.array ~= 30; a.array ~= 99; a.array ~= 1; writefln("sorted:"); foreach (int i, uint u; a) { writefln("%d: %d", i, u); } writefln("\nreverse sorted:"); foreach (int i, Foo.ReverseFoo u; a) { writefln("%d: %d", i, u); } writefln("\nunsorted:"); foreach (int i, Foo.UnsortedFoo u; a) { writefln("%d: %d", i, u); } writefln("\nrandom A:"); foreach (int i, Foo.RandomFoo u; a) { writefln("%d: %d", i, u); } writefln("\nrandom B:"); foreach (int i, Foo.RandomFoo u; a) { writefln("%d: %d", i, u); } } //---------------------------- </code> -- Derek Melbourne, Australia 8/06/2005 11:25:24 AM |
June 08, 2005 Re: Overload In | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek, this is an excellent example - no need for factories etc... Perfect! Generally speaking - this approach can be used almost in all cases where (simple) factories are used.
--
...........
Dejan Lekic
http://dejan.lekic.org
|
Copyright © 1999-2021 by the D Language Foundation