Find out whether a container is a class or an object

156 views Asked by At

I was curious about grammars being classes or singletons, so I created this small program to find out:

grammar Mini {
    token TOP { \* <word> \* }
    token word { \w+ }
}

proto sub is-class( | ) { * };
multi sub is-class( Grammar:D $g ) { return "Object" };
multi sub is-class( Grammar:U $g ) { return "Class" };

say is-class( Mini );

This uses multiple dispatch to find that out, and it turns out that Mini is actually a class. In general, would there be a shorter way of finding this out? Or a way that would not require to know the actual class of which the package might be an instance?

1

There are 1 answers

2
Christoph On

You can disambiguate 'instances' and 'classes' via DEFINITE, ie

Mini.DEFINITE ?? 'Object' !! 'Class'

or rather

Mini.DEFINITE ?? 'concrete object' !! 'type object'

should do the trick.