有没有办法在 Perl 6 程序中获取所有已知类型的列表? [英] Is there a way to get a list of all known types in a Perl 6 program?

查看:33
本文介绍了有没有办法在 Perl 6 程序中获取所有已知类型的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得 Perl 6 程序知道的所有已知类型(内置、定义、加载等等)的列表?我脑子里没有特定的任务,这与确定是否已经定义了我已经知道的类型有点不同.

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.

推荐答案

这应该可以解决问题:

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

说明:

Perl 6 提供了允许间接查找符号的伪包在不同的范围内声明/可见.它们可以像哈希一样被访问和迭代.

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.

  • 所有内置符号都应该在 CORE:: 中.
  • 查找在当前词法作用域或其父作用域之一中声明(或导入到)中的所有那些更棘手.
    根据文档中的描述,我原以为 LEXICAL:: 将包含所有这些,但基于一些似乎并非如此的实验,它看起来像 UNIT::OUTERS::MY:: 需要被搜索以捕获它们.
  • 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:

  • 类型(包、模块、类、角色、原生类型、枚举类型、子集类型)
  • 函数(子程序、术语和运算符)
  • 枚举值
  • 变量和常量

为了仅获取类型,我搜索了符号声明名称等于其对象类型名称的类型.

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' })

这篇关于有没有办法在 Perl 6 程序中获取所有已知类型的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆