July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | On Tue, Jul 7, 2009 at 9:48 AM, Derek Parnell<derek@psych.ward> wrote:
> On Tue, 07 Jul 2009 11:01:12 -0500, Andrei Alexandrescu wrote:
>
>> To me they look the same, but if people are happier with wasting vertical space, sure.
>
> I think I've finally worked out why I don't like this syntax. Consider this ...
>
>
> switch (X) {
> case 1: funcA(); break;
> case 2,5,7,10 .. 17, 24, 32: funcB(); break;
> case 3,6,8,18 .. 23: funcC(); break;
> }
>
> In other words, allowing a range of value inside a list of values.
>
> How is this possible in today's D?
By adding the word "case" several more times.
I really wish we would just go with two-dots for exclusive, three-dots for inclusive, and make that work consistently everywhere.
The "too similar to see the difference" argument just doesn't hold weight with me. Unless you're programming in a proportional-width font the difference is pretty obvious. Or define some other char sequence to mean inclusive range. Like a ..|b or a..:b or a..^b, etc.
--bb
| |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > I really wish we would just go with two-dots for exclusive, three-dots > for inclusive, and make that work consistently everywhere. > > The "too similar to see the difference" argument just doesn't hold > weight with me. Unless you're programming in a proportional-width > font the difference is pretty obvious. That is troublesome because it introduces very weird semantics driven by counting dots and inserting whitespace. Consider: int wyda; void main() { int wyda; foreach (i; 0...wyda) { .... } } Now there are several problems with this. All of the following compile: 0.....wyda 0....wyda 0... .wyda 0. .. .wyda 0.... wyda 0. ...wyda 0. ....wyda 0.. .wyda and many others too. Using "..." as a separator in addition to a trailing symbol is bound to be a complete disaster. > Or define some other char > sequence to mean inclusive range. Like a ..|b or a..:b or a..^b, > etc. That is more sensible. But as far as switch is concerned, I'm thoroughly content with what we have now, modulo the 256 limit. Andrei | |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter:
>Or define some other char sequence to mean inclusive range. Like a ..|b or a..:b or a..^b, etc.<
The Chapel language uses:
..#
That I think is better than all your alternatives. But I think Andrei doesn't like this too.
Bye,
bearophile
| |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | Leandro Lucarella wrote: > I think the ... operator makes perfect sense because it's more general, > yo could use array[X...Y] and case X..Y: where appropriate. and > doesn't look so bad though, but I think it could be easy to miss the ".." > when reviewing the code If it's easy to miss .., then it ought to be really easy to miss the distinction between .. and ... ! | |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> Bill Baxter:
>> Or define some other char sequence to mean inclusive range. Like a ..|b or a..:b or a..^b, etc.<
>
> The Chapel language uses:
> ..#
> That I think is better than all your alternatives. But I think Andrei doesn't like this too.
I like:
a .. b+1
to mean inclusive range.
| |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright Wrote:
> I like:
> a .. b+1
> to mean inclusive range.
Me too, but does it work when the upper interval is int.max, char.max, ulong.max, ucent.max, etc?
Bye,
bearophile
| |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright, el 7 de julio a las 10:46 me escribiste: > Leandro Lucarella wrote: > >I think the ... operator makes perfect sense because it's more general, yo could use array[X...Y] and case X..Y: where appropriate. > > and > > >doesn't look so bad though, but I think it could be easy to miss the ".." when reviewing the code > > If it's easy to miss .., then it ought to be really easy to miss the distinction between .. and ... ! As another people said, use another way to write inclusive range, but keep things consistent =) -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- UNA ARTISTA HACE JABONES CON SU PROPIA GRASA LUEGO DE UNA LIPOSUCCION -- Crónica TV | |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | Andrei Alexandrescu wrote: > Now there are several problems with this. All of the following compile: Possible solution: treat all sequences as dots as part of the same token, always. > 0.....wyda > 0....wyda > 0.... wyda > 0... .wyda > 0.. .wyda > 0. ....wyda All rejected by the lexer. > 0. .. .wyda > 0. ...wyda Legal and unambiguous. -- Rainer Deyke - rainerd@eldwood.com | |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Rainer Deyke | Rainer Deyke wrote: > Andrei Alexandrescu wrote: >> Now there are several problems with this. All of the following compile: > > Possible solution: treat all sequences as dots as part of the same > token, always. > >> 0.....wyda >> 0....wyda >> 0.... wyda >> 0... .wyda >> 0.. .wyda >> 0. ....wyda > > All rejected by the lexer. No. Actually, some, yes :o). >> 0. .. .wyda >> 0. ...wyda > > Legal and unambiguous. And throroughly confusing, which was my point. Andrei | |||
July 07, 2009 Re: Case Range Statement .. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> Walter Bright Wrote:
>> I like:
>> a .. b+1
>> to mean inclusive range.
>
> Me too, but does it work when the upper interval is int.max, char.max, ulong.max, ucent.max, etc?
Nope. Wrap around is always a special case, not just for ranges. It's in the nature of 2's complement arithmetic.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply