Azurewrath

Hi,

Is there a construct like this in C#

case of

(

(if a > 5): A();

(if b < 4): B();

)

If not, will there be in C# 3

Thanks,

aw



Re: Visual C# Language Case of construct

OmegaMan

What language are you pulling that out of That looks like lisp....





Re: Visual C# Language Case of construct

Marcelo Guerra - MSFT

Hi,

The answer is no and no Smile. Can you give more info on this construct , why do you find it usefull

Thanks,

Marcelo.





Re: Visual C# Language Case of construct

Azurewrath

Wow. OmegaMan you hit the nail on the head. Can't believe you guessed it. It's actually a language called MAXScript, a script language for 3ds max, which is inspired from Lisp. In MAXScript, everything is an expression. It has really some neat features.

You can check out the complete reference (grammar, features, examples, etc) by downloading the help file here (last link):

http://usa.autodesk.com/adsk/servlet/index siteID=123112&id=5659551

From the help:

Case Expression

The case expression is used to select an expression to be evaluated from a set of labeled expressions based on a test value compared against the labels.

Only the first expression whose label matches the test expression is evaluated. All labels must be comparable to the test expression. Labeled expressions can be block-expressions, so you can use the case expression as a case statement, choosing between blocks of code to execute.

The syntax for <case_expr> is:

case [ <expr> ] of ( <cases> )

where <expr> is the test expression and <cases> is a sequence of labeled expressions:

<factor>: <expr>

default: <expr>

Examples

new_obj = case copy_type.state of

(

2: copy $foo

3: instance $foo

default: reference $foo

)

A special label, default, can be optionally used to tag the expression that will be evaluated if no other labels match the test expression. If the default label is not used, and no label matches the test expression, the case expression returns a value of undefined.

The labels are <factors> - elements such as number literals, name literals, string literals, variables, or block-expressions. When a case expression is evaluated, the test expression is evaluated once, then each label expression is evaluated in order until one is found that compares equally to the test expression value. The expression it labels is then evaluated and this becomes the result of the case expression. No further labels are evaluated or tested.

The test expression is optional. If it is not supplied, the labels are expected to be Boolean values or expressions, and the first label evaluated as true determines the chosen case. In this variant, it is common to use expressions as labels, so be sure to parenthesize them.

For example:

case of

(

(a > b): print "a is big"

(b < c): print "b is little"

(c <= d*3): ...

default: ...

)

Thanks,

aw





Re: Visual C# Language Case of construct

Mark Dawson

Hi,

from reading the explanation it seems that in the example below:

case of

(

(if a > 5): A();

(if b < 4): B();

)

In C# it translates to:

if(a>5) { A(); }

else if(b<4) { B(); }

probably not a big enough win in terms of readability / efficiency to add this to the language. Do you find it useful, or beneficial in certain cases

Mark.






Re: Visual C# Language Case of construct

OmegaMan

I actually understand the concept but the application would be unique. The closest one could approximate it would be create a generic struct to hold two delegates. The first delegate method would be the test and the second would be the operations.

Those delegate pairs would have to be in a list and then that list enumerated until the a test returned positive. The default would be the last item on the list where the test would simply return true.




Re: Visual C# Language Case of construct

Azurewrath

Thanks guys. Great answers.

Mark, you are right, I only give a small example, but the benefit of using this happens, when you have several cases. Also I should mention that this is not a fall-through construct like in c++. I know switch isn't fall-through in c# too, right But still you have to use break. So this has 2 flavors:

case val of

(

1: A()

2: B()

"C": C()

)

or the example in my first pos.

Just as an example, this language also have:

for i in array do i

which is the same as foreach in C#, which I really like.

But here is another, that works on all for loops (collect):

newArray = for i = 1 to array.count collect (arrayIdea + 2)

which makes another array.

And you can filter the result using a where construct, but C# already can use multiple test conditions, right So that's good.

Thanks,

aw