Where should I put a `condp` like function?

Idan Arye GenericNPC at gmail.com
Mon Apr 15 10:53:34 PDT 2013


On Monday, 15 April 2013 at 17:07:22 UTC, Nick Sabalausky wrote:
> On Mon, 15 Apr 2013 18:41:07 +0200
> "Idan Arye" <GenericNPC at gmail.com> wrote:
>> 
>> Having D's `switch` be an expression is problematic, since D 
>> does not have the convention of 
>> blocks-returning-the-value-of-the-last-statement, and since D 
>> is statically typed - adding this might cause unwanted 
>> implicit conversions.
>> 
>> Maybe it could be pulled off with a syntax similar to Scala:
>>      switch(x){
>>          case 1 => ...
>>          case 2 => ...
>>      }
>> 
>
> Wouldn't it be possible to just simply choose between these two 
> forms
> based on whether the switch is used where an expression is 
> expected
> versus where a statement would be accepted?:
>
> switch(cond) {
> case 1: [...statements...] break;
> ...
> }
>
> vs
>
> switch(cond) {
> case 1: [...expression...];
> ...
> }

Possible? probably. Simply? probably not. `switch` is already a 
statement, so it will have to be added as an expression, but 
expressions in D can be used as statements, so it could create an 
ambiguity in the syntax parser - and those are seldom easy to 
solve.

The `switch`+`=>` solution should be simpler, since it doesn't 
create that ambiguity, but syntax additions are never that simple.

`predSwitch`, on the other hand, is dead simple.


More information about the Digitalmars-d mailing list