Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a list of all known types in a Perl 6 program?

Tags:

types

raku

Is there a way to get a list of all known types (builtin, defined, loaded, whatever) a Perl 6 program knows about? I don't have a particular task in mind, and this is a bit different than figuring out if a type I already know about has been defined.

like image 909
brian d foy Avatar asked Jul 01 '17 13:07

brian d foy


Video Answer


1 Answers

This should do the trick:

.say for (|CORE::, |UNIT::, |OUTERS::, |MY::)
    .grep({ .key eq .value.^name })
    .map(*.key)
    .unique
;

Explanation:

Perl 6 provides Pseudo-packages that allow indirect look-up of symbols that are declared/visible in different scopes. They can be accessed and iterated like Hashes.

  • All built-in symbols should be in CORE::.
  • Finding all those declared in (or imported into) the current lexical scope or one of its parent scopes, is more tricky.
    Based on its description in the documentation, I would have thought LEXICAL:: would contain them all, but based on some experimentation that doesn't seems to be case and it looks like UNIT::, OUTERS::, and MY:: need to be searched to catch 'em all.

The kind of symbols defined in those pseudo-packages include:

  • types (packages, modules, classes, roles, native types, enum types, subset types)
  • functions (subroutines, terms and operators)
  • enum values
  • variables & constants

To get only the types, I grepped the ones where the symbol's declared name equals the name of its object type.

If you wanted only classes, you could add the following step:

    .grep({ .value.HOW.^name eq 'Perl6::Metamodel::ClassHOW' })
like image 169
smls Avatar answered Sep 23 '22 11:09

smls