1

I have an enumerated abstract class:

shared abstract class Foo() of bar|baz {}

And a function that attempts to check if a Foo is not a bar:

shared void test(Foo foo) {
    if (!is bar foo) {

    }
}

I get the error

incorrect syntax: no viable alternative at token 'bar'
Papershine
  • 4,995
  • 2
  • 24
  • 48

2 Answers2

3

@gdejohn's answer is correct, but I would also note that it doesn't usually make much sense to directly refer to the type of an enumerated instance. Usually you would write the code like this:

void test(Foo foo) {
    if (foo!=bar) {
        print("Not bar!");
    }
}
Gavin King
  • 3,182
  • 1
  • 13
  • 11
  • 1
    Agreed that you usually don't care to narrow the type. The situation where it would make sense is if `bar` or `baz` declared new attributes or methods that you wanted to use. – gdejohn Aug 14 '17 at 18:51
  • 1
    There's another even more interesting usecase: where you want to narrow the set of options down to a subset of the instances, for example, a type like: `\Ioption1|\Ioption4|\Ioption5`. – Gavin King Aug 15 '17 at 21:31
2

Because bar is an enumerated instance, it's just a value, not a type. But it does have a more specific type than Foo, which you can denote by prefixing it with \I.

void test(Foo foo) {
    if (!is \Ibar foo) {
        print("Not bar!");
    }
}
gdejohn
  • 7,451
  • 1
  • 33
  • 49