March 11, 2008 Re: Comparing Multiple Values | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to Derek,
>
>> On Tue, 11 Mar 2008 13:39:55 +0100, downs wrote:
>>
>>> There's a better way actually.
>>>
>> But that is only good for ranges ;-)
>>
>> Not so good for ...
>>
>> if value in set(2,4,7,10,23,445)
>>
>> I'm pretty sure this will be simple to organize once AST macros are
>> implemented.
>>
>
> struct _Set(T) {
> T[] set;
> bool opIn_r(U)(U u) {
> foreach(v; set) if(v==u) return true;
> return false;
> }
> }
>
> struct Set {
> static _Set!(T) opIndex(T, U)(T[] a...) {
> _Set!(T) ret;
> ret.set = a.dup; //need the dup???
> return ret;
> }
> }
>
> I /think/ that will work. But I haven't tested it.
>
> if(1 in Set[1,2,3,5,9,23])
Hadn't noticed that you wrote basically the same thing I did already ...
You left a U template parameter in the opIndex.
The dup isn't necessary.
And unfortunately it doesn't work because IFTI isn't smart enough to deduce the template argument. So you need to call it as Set[[1,2,3,4]].
--bb
|
March 12, 2008 Re: Comparing Multiple Values | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Reply to Bill, > Hadn't noticed that you wrote basically the same thing I did already > ... > > You left a U template parameter in the opIndex. Errr. Oops. > The dup isn't necessary. > And unfortunately it doesn't work because IFTI isn't smart enough to > deduce the template argument. So you need to call it as > Set[[1,2,3,4]]. that's a cool syntax <<g>> > --bb > |
March 12, 2008 Re: Comparing Multiple Values (the story so far/summary) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter Wrote:
> downs wrote:
> > Bill Baxter wrote:
> >> BCS wrote:
> >>> Reply to Downs,
> >>>
> >>>> Bill Baxter wrote:
> >>>>
> >>>>> downs wrote:
> >>>>>
> >>>>>> `in` already is an infix operator :)
> >>>>>>
> >>>>>> --downs
> >>>>>>
> >>>>> Yes, but it doesn't work!
> >>>>>
> >>>>> --bb
> >>>>>
> >>>> Curious.
> >>>>
> >>>> Works here, GDC 1.028 / 4.1.2
> >>>>
> >>>> What are you using?
> >>>>
> >>>> --downs
> >>>>
> >>> I think he was saying the "in" operator dosn't work / works wrong.
> >> Right, as in "a in [1,2,3]" is an error, rather than doing what the poster who started this thread expected it to do.
> >>
> >
> > To give an example of W's stance ..
> >
> > Say you have a book with four pages.
> >
> > The first page contains the number 15, the second page 16 the third 23 and the fourth 42.
> >
> > Now, if I ask "3 in book", do I mean "is the page 3 in the book" (index) or "is the number 3 in the book" (value)?
> >
> > There's precedence in the D language for the first case, because `in` for AAs also checks against index (key) and not value.
> >
> > However, checking against value would arguably be more useful :)
> >
> > In the absence of a consensus, and because a case could be made for both possibilities, it was decided to leave it out for now.
> >
> > That should about sum it up.
>
> Yes, that sums it up quite nicely.
>
> --bb
Thanks for clearing up why it's not in there. I was really expecting it to work as I thought, however it's no big deal. Thanks everyone!
|
Copyright © 1999-2021 by the D Language Foundation