Friday, March 04, 2011

Xtend Screencast Part 2 - Switch Expression

In this part I show and explain the switch expression, which is defined in Xbase, so you can also have that in you Xtext DSL if you want :-)
In the tutorial at EclipseCon on Thursday we will show how to embed Xbase expressions in a simple DSL.

Here again I use Xtend for the demo:

4 comments:

  1. Looks cool :)

    So, the output of

    val l = newArrayList(true, false)
    for (s: l) {
    switch(s) {
    case s == false: println(s);
    }
    }

    is "false" and not empty?

    ReplyDelete
  2. Yes, but we will likely add a compile error if you switch over boolean because it doesn't make sense and to avoid this kind of confusion.

    Generally spoken: If the guard is of type boolean, the value in the switch is ignored.
    You could have written:

    val l = newArrayList(true, false)
    for (s: l) {
    switch('foo') {
    case s == false: println(s);
    }
    }

    and it would have the same effect.

    ReplyDelete
  3. An error or a warning could be a life-saver IMHO. Looking forward to the upcomming episodes :)

    ReplyDelete
  4. I'm really looking forward to seeing a small generator example. So far this looks like "Java - How it should be" =)

    ReplyDelete