3

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

Robert P
  • 15,707
  • 10
  • 68
  • 112

2 Answers2

11

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/

user56602
  • 111
  • 1
  • 2
  • Thanks, this is very helpful, now only if SO would send me an e-mail notifying you answered it. –  Jan 26 '09 at 11:43
  • 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 ;) ) – user56602 Jan 30 '09 at 05:30
  • Good answer. Note that if you also want to support "when... else", it'll get trickier. – Avish Aug 31 '09 at 20:42
0

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
  • 50,694
  • 11
  • 78
  • 122
  • 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). –  Jan 16 '09 at 12:29