digitalmars.D.learn - Overload In
- John Demme (5/5) Jun 05 2005 This seems like a stupid question, and sorry if it's been covered
- Chris Sauls (4/7) Jun 05 2005 I thought it was overloadable, but checking the documentation I couldn't...
- Hasan Aljudy (4/15) Jun 07 2005 hmm, there's an opApply for use with "foreach".
- Chris Sauls (6/9) Jun 07 2005 The opApply overload is for making classes/structs foreach-able. It
- Hasan Aljudy (9/22) Jun 07 2005 Well, I just thought that, you know .. "foreach" means "foreach x in y",...
- Derek Parnell (119/124) Jun 07 2005 Here is a longer example code that might help..
- Dejan Lekic (8/8) Jun 08 2005 Derek, this is an excellent example - no need for factories etc... Perfe...
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
Jun 05 2005
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
Jun 05 2005
Chris Sauls wrote:John Demme 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.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
Jun 07 2005
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
Jun 07 2005
Chris Sauls wrote:Hasan Aljudy wrote: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.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
Jun 07 2005
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
Jun 07 2005
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
Jun 08 2005