Another switch suggestion.

Fredrik Olsson peylow at gmail.com
Sun Sep 3 03:24:46 PDT 2006


Stewart Gordon skrev:
> Fredrik Olsson wrote:
<snip>
> I'm not sure I like your idea really.  Moreover, suppose you have this:
<snip>
> Moreover, your code could just as easily be written
> 
>     if ((foo >= 'a' && foo <= 'z') || (foo >= 'A' && foo <= 'Z')) {
Never mind the use of $, pick any character or keyword you like. It was 
an example of how the existing switch statement could be extended to 
solve, among some things, the tedious typing of the example you also 
wrote down.

Visual Basic uses the keyword Is for this, C# reserver value in property 
setters, lisp uses ** for previous results, etc. I do not much care what 
D would use, only if it solves the problem. I picked $ for the example 
as it is already used as a "special placeholder". If it is not 
practical, then fine use something else.

I still think the idea behind is good as it will reduce code size and 
make source more readable. Less code == less bugs, more readable == more 
maintainable.


>> One of the few features I miss from Visual Basic :).
>>
>> And an even better syntax for the above example would naturally be:
>> switch (foo) {
>>   case $ in <'a'..'z', 'A'..'Z'>:
> <snip>
> 
> Why have the '$' at all for this syntax?  Or are you trying to propose a 
> whole new expression syntax to be used in more than just cases?  Using 
> '<' and '>' for anything but comparison operators is bound to lead to 
> parsing complications.  That's why templates don't use this notation.
> 
Yes, this is yet again a proposal to add sets and ranges from pascal to 
D. It should not just work for cases. A language construct that only 
works in one construct is pretty useless. A great language have many 
small constructs solving specific problems, that can be combined to 
infinity to solve any problem. Naturally sets, if included, should be 
usable in any context.

So <1, 42> was only my small example of how a set literal could be 
constructed. But if we are top use Chris Miller's suggestions for array 
and struct literals, then the set literal would probably be int<>!<1, 
42> instead ( using {} for struct content, [] for arrays, and <> for 
sets, seems plausible too me).


// Fredrik Olsson



More information about the Digitalmars-d mailing list