Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out whether a container is a class or an object

Tags:

raku

mop

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?

like image 611
jjmerelo Avatar asked Oct 09 '18 05:10

jjmerelo


1 Answers

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.

like image 68
Christoph Avatar answered Oct 16 '22 05:10

Christoph