Closed nddrylliog closed 11 years ago
foo := 1;
match foo {
case 1 =>
}
Builds and runs just fine. Not sure empty case is the problem. Btw, I have no idea what happens with that colon and class (: Baz
) there, couldn't find documentation on that either?
$ rock --version
rock 0.9.6 codename loki, built on Wed Mar 13 22:57:47 2013
@jussi-kalliokoski What happens with that colon and class is that the object is checked for its type, and cast to the appropriate target.
For example:
kill: func (entity: Entity) {
match entity {
case hero: Hero =>
// I refuse to kill the hero!
hero cheer()
case enemy: Enemy =>
enemy die()
}
}
This is akin to Scala pattern matching, although much weaker/simpler.
What made my example blow up I think is that the match was used as a value (for example, as the return expression of a function.. or a variable decl.. or anything really).
So rock is looking for an expression in the case to assign to the temp value, and it fails.
Example:
a := match 1 {
case 1 => // there's nothing there, blow up!
}
If it was correct, e.g.:
a := match Random randInt(1, 2) =>
case 1 =>
"blah"
case 2 =>
"blih"
Then it would compile down to something like this:
a: String
r := Random randInt(1, 2)
if (r == 1) {
a = "blah"
} else if (r == 2) {
a = "blih"
}
Except not really because of #615, but that's a separate issue :)
Ahhh, ok, that makes sense, thanks. But not sure the example should even compile to anything, but the current behavior admittedly isn't desirable either. It would be good to get a descriptive error for this.
Ah no the current example should err with 'no usable value in case inside a match used as expression' or some other less terrible error message. In any case it should definietly not crash rock.
Will be on it tomorrow
@shamanas Welcome back!
@nddrylliog Happy to be here again! o/
I can't seem to reproduce this on pacino (.9.7.x), I only get "Couldn't figure out type of match" error (which is tagged as a serious error that should be reported)
Well.. then I don't know exactly what piece of code caused it, but in any case a check should be added when looking for a Scope's last element - if the Scope is empty => compile error.
Complete test case:
Baz: class {}
foo := Baz new()
match foo {
case bar: Baz =>
}
Closed in 97x.
If you have a match like this:
rock will fail with:
[OutOfBoundsException in ArrayList]: Trying to access an element at offset 0, but size is only 0!