tags:

views:

211

answers:

2

Hi,

I cannot seem to figure out how I can add the 'when' keyword to BOO which should behave as an 'if'. I figure I could make a method, but then I cannot move the when around like I can with if. Any pointers would be appreciated.

-Mark

A: 

This would be a job for a macro. From the page you linked to, it seems that Boo has syntactic macros.

As an aside note, why do you need an exact duplicate of an existing functionality?

Svante
The page linked to was an edit :) I saw that and all but for the if functionality I was unable to get it to work.Reason why I want it is for a DSL (readability).
+10  A: 

This would do what you want:

import Boo.Lang.Compiler.Ast
import Boo.Lang.Compiler.MetaProgramming

macro when:
    return [|
        if $(when.Arguments[0]):
            $(when.Block)
    |]

x = 1
when x == 1:
    print "x equals one"
when x == 2:
    print "x equals two"

Btw, feel free to (also) ask on the Boo mailing-list to get (more) answers quicker ;)

http://groups.google.com/group/boolang/

Thanks, this is very helpful, now only if SO would send me an e-mail notifying you answered it.
Hehe, well that's why I advised you to subscribe to the Boo mailing-list for your future questions (or just to keep up with the updates of the language ;) )
Good answer.Note that if you also want to support "when... else", it'll get trickier.
Avish