digitalmars.D - The in operator and normal arrays
- Myron Alexander (11/11) Apr 25 2007 Hello.
- Derek Parnell (22/35) Apr 25 2007 No. The D programming language does not contain a reserved word or built...
- Myron Alexander (4/6) Apr 25 2007 Thanks Derek.
- Bill Baxter (6/22) Apr 25 2007 I've proposed this before. Maybe others too. It makes sense. It's
- Myron Alexander (7/13) Apr 25 2007 I suspected as much. Thanks for the info. I add my voice of support for
- Bill Baxter (5/22) Apr 25 2007 I don't recall if I ever filed an enh request for it. If you have time,...
- Myron Alexander (9/14) Apr 27 2007 Bill,
- Frits van Bommel (6/11) Apr 27 2007 You can only overload operators on structs and classes (and perhaps
- Myron Alexander (21/34) Apr 27 2007 Hello Frits.
- Jarrett Billingsley (4/7) Apr 27 2007 Change it to opIn_r and it will work. (Personally I think the forward a...
- Myron Alexander (2/6) Apr 27 2007 Thanks Jarrett, works now.
- Downs (6/16) Apr 26 2007 Agreed. If only because it is expected to work that way, and didn't we l...
- Dan (2/22) Apr 26 2007
- Vladimir Panteleev (5/7) Apr 26 2007 Or, better yet, just get fully-working sets (like dynamic arrays but wit...
Hello. I'm curious as to the reason for why the in operator only works with associative arrays. I am working on a program where having an in operator for a normal array would be useful. eg: if ("string" in ["string1", "string2", "string3", ...]) { do something } Is there some other way to do this in the language without resorting to a function or class method call? Thanks ahead, Myron Alexander.
Apr 25 2007
On Thu, 26 Apr 2007 01:30:46 +0200, Myron Alexander wrote:Hello. I'm curious as to the reason for why the in operator only works with associative arrays. I am working on a program where having an in operator for a normal array would be useful. eg: if ("string" in ["string1", "string2", "string3", ...]) { do something } Is there some other way to do this in the language without resorting to a function or class method call?No. The D programming language does not contain a reserved word or built-in function to scan through an array other than Associative Arrays. The simplest is just a function call ... if (container.contains(elem) == true) { // do something } where contains is something along the lines of ... bool contains(T)(T[] container,T elem) { foreach(e; container) if (e == elem) return true; return false; } Unless you know some details about the internal ordering of the array elements, a simple linear scan is all that can really be done. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 26/04/2007 9:33:19 AM
Apr 25 2007
Derek Parnell wrote:No. The D programming language does not contain a reserved word or built-in function to scan through an array other than Associative Arrays.Thanks Derek. Regards, Myron Alexander.
Apr 25 2007
Myron Alexander wrote:Hello. I'm curious as to the reason for why the in operator only works with associative arrays. I am working on a program where having an in operator for a normal array would be useful. eg: if ("string" in ["string1", "string2", "string3", ...]) { do something } Is there some other way to do this in the language without resorting to a function or class method call? Thanks ahead, Myron Alexander.I've proposed this before. Maybe others too. It makes sense. It's what other languages with an 'in' operator do. It should work. It doesn't. It isn't high on Walter's priority list because it can be worked around easily. --bb
Apr 25 2007
Bill Baxter wrote:I've proposed this before. Maybe others too. It makes sense. It's what other languages with an 'in' operator do. It should work. It doesn't. It isn't high on Walter's priority list because it can be worked around easily. --bbI suspected as much. Thanks for the info. I add my voice of support for the proposal to expand the in operator to operate on normal arrays. This is something that would be nice to have, but as you say, the work-around is simple enough. Thanks, Myron Alexander.
Apr 25 2007
Myron Alexander wrote:Bill Baxter wrote:I don't recall if I ever filed an enh request for it. If you have time, you could check and see, and then file one if you don't find it there already. --bbI've proposed this before. Maybe others too. It makes sense. It's what other languages with an 'in' operator do. It should work. It doesn't. It isn't high on Walter's priority list because it can be worked around easily. --bbI suspected as much. Thanks for the info. I add my voice of support for the proposal to expand the in operator to operate on normal arrays. This is something that would be nice to have, but as you say, the work-around is simple enough. Thanks, Myron Alexander.
Apr 25 2007
Bill Baxter wrote:I don't recall if I ever filed an enh request for it. If you have time, you could check and see, and then file one if you don't find it there already. --bbBill, I was in the process of writing the enhancement request when I had a quick peek at the documentation. The operator overloading document specifically allows for overloading the in operator with opIn and opIn_r (although doesn't say how it is supposed to be used). This got me thinking, is this an enhancement request, or a bug fix request? Regards, Myron.
Apr 27 2007
Myron Alexander wrote:I was in the process of writing the enhancement request when I had a quick peek at the documentation. The operator overloading document specifically allows for overloading the in operator with opIn and opIn_r (although doesn't say how it is supposed to be used). This got me thinking, is this an enhancement request, or a bug fix request?You can only overload operators on structs and classes (and perhaps unions). You can't overload operators purely on built-in types and arrays. (You /could/ overload it separately for every aggregate type you implement, but then it still won't work for arrays of primitive types, or arrays of arrays)
Apr 27 2007
Frits van Bommel wrote:Myron Alexander wrote:Hello Frits. I am not sure how to use the in operator. I tried to overload it in a class and struct but received a compiler error. This is the code I tried: struct A { int opIn (char[] val) { return true; } } void main () { A a = A (); if ("hello" in a) { printf ("It's alive\n"); } } The compiler error I received: inop.d(10): Error: rvalue of in expression must be an associative array, not A How do I overload the in operator? Thanks ahead, Myron.I was in the process of writing the enhancement request when I had a quick peek at the documentation. The operator overloading document specifically allows for overloading the in operator with opIn and opIn_r (although doesn't say how it is supposed to be used). This got me thinking, is this an enhancement request, or a bug fix request?You can only overload operators on structs and classes (and perhaps unions). You can't overload operators purely on built-in types and arrays. (You /could/ overload it separately for every aggregate type you implement, but then it still won't work for arrays of primitive types, or arrays of arrays)
Apr 27 2007
"Myron Alexander" <dprogramming myron.RMVETHISalexander.com> wrote in message news:f0tona$38c$1 digitalmars.com...int opIn (char[] val) { return true; }Change it to opIn_r and it will work. (Personally I think the forward and reverse overloads should be switched, but whatever)
Apr 27 2007
Jarrett Billingsley wrote:Change it to opIn_r and it will work. (Personally I think the forward and reverse overloads should be switched, but whatever)Thanks Jarrett, works now.
Apr 27 2007
Myron Alexander wrote:Bill Baxter wrote:Agreed. If only because it is expected to work that way, and didn't we learn that breaking expectations is Sin? In the meantime, a workaround with a comparable functionality as opIn would be T *has(T)(T[] array, T match) { foreach (inout elem; array) if (elem==match) return &elem; return null; } void main() { auto a=[0, 1, 2, 3]; if (&a[3] is a.has(3)) writefln("OK"); } Greetings ^^I've proposed this before. Maybe others too. It makes sense. It's what other languages with an 'in' operator do. It should work. It doesn't. It isn't high on Walter's priority list because it can be worked around easily. --bbI suspected as much. Thanks for the info. I add my voice of support for the proposal to expand the in operator to operate on normal arrays.
Apr 26 2007
inFavor++; Downs Wrote:Myron Alexander wrote:Bill Baxter wrote:Agreed. If only because it is expected to work that way, and didn't we learn that breaking expectations is Sin? In the meantime, a workaround with a comparable functionality as opIn would be T *has(T)(T[] array, T match) { foreach (inout elem; array) if (elem==match) return &elem; return null; } void main() { auto a=[0, 1, 2, 3]; if (&a[3] is a.has(3)) writefln("OK"); } Greetings ^^I've proposed this before. Maybe others too. It makes sense. It's what other languages with an 'in' operator do. It should work. It doesn't. It isn't high on Walter's priority list because it can be worked around easily. --bbI suspected as much. Thanks for the info. I add my voice of support for the proposal to expand the in operator to operate on normal arrays.
Apr 26 2007
On Thu, 26 Apr 2007 03:09:46 +0300, Myron Alexander <consulting myron.RMVETHISalexander.com> wrote:I suspected as much. Thanks for the info. I add my voice of support for the proposal to expand the in operator to operate on normal arrays.Or, better yet, just get fully-working sets (like dynamic arrays but without values). Declared like void[datatype], you could do the same operations as with AA (in, .remove, foreach, etc.), plus .add(element) (the AA syntax won't work here). These would work much faster anyway, at the cost of preserving a certain order and having at most one instance of an element at a time. -- Best regards, Vladimir mailto:thecybershadow gmail.com
Apr 26 2007