ruby - 获取有关 Crystal 中继承链的信息

标签 ruby inheritance reflection metaprogramming crystal-lang

出于好奇并想稍微了解一下 Crystal 的一般结构,我一直在寻找一些反射功能,这些功能可以让我更好地理解继承链的构建方式。

我在想像 ruby​​ 的 superclass 这样的东西, ancestorsincluded_modules方法。

在 Crystal 语言中有类似的东西吗?

此外,拥有某种可以向我展示全局的图表会很有用。

最佳答案

Crystal 中的元编程是使用 macros 实现的.

Macros are methods that receive AST nodes at compile-time and produce code that is pasted into a program.

Crystal 已经实现了 superclassancestors在编译时返回结果。所以你可以这样做:

{{ pp MyClass.superclass }}
{{ pp MyClass.ancestors }}

为方便起见,您可以编写自己的宏来检查继承。考虑到学习目的,它可能看起来像这样:

class Class
  def superclass
    {{ @type.superclass }}
  end

  def ancestors
    {% if @type.ancestors.size > 0 %}
      {{ @type.ancestors }}
    {% else %}
      [] of Nil
    {% end %}
  end

  def included_modules
    {% if @type.ancestors.any? { |a| a.class.stringify.ends_with?(":Module") } %}
      {{ @type.ancestors.select { |a| a.class.stringify.ends_with?(":Module") } }}
    {% else %}
      [] of Nil
    {% end %}
  end

  def inheritance_chain
    String.build do |chain|
      cls = self
      chain << cls
      while !(cls = cls.superclass).nil?
        chain << " > #{cls}"
      end
    end
  end
end

然后就可以做检查了:

class A
end

module B
end

require "crystal/system/random"

class C < A
  include B
  include Crystal::System::Random
end

C.name             # => "C"
C.superclass       # => A
C.ancestors        # => [Crystal::System::Random, B, A, Reference, Object]
C.included_modules # => [Crystal::System::Random, B]
A.included_modules # => []

如果你更进一步:

C.superclass                                                    # => A
C.superclass.try &.superclass                                   # => Reference
C.superclass.try &.superclass.try &.superclass                  # => Object
C.superclass.try &.superclass.try &.superclass.try &.superclass # => nil

现在使用 inheritance_chain:

Int32.inheritance_chain                          # => "Int32 > Int > Number > Value > Object"
String.inheritance_chain                         # => "String > Reference > Object"
Float64.inheritance_chain                        # => "Float64 > Float > Number > Value > Object"
Array(Bool).inheritance_chain                    # => "Array(Bool) > Reference > Object"
Hash(Bool, Bool).inheritance_chain               # => "Hash(Bool, Bool) > Reference > Object"
Tuple(Char).inheritance_chain                    # => "Tuple(Char) > Struct > Value > Object"
NamedTuple(s: String, b: Bool).inheritance_chain # => "NamedTuple(s: String, b: Bool) > Struct > Value > Object"
Nil.inheritance_chain                            # => "Nil > Value > Object"
Regex.inheritance_chain                          # => "Regex > Reference > Object"
Symbol.inheritance_chain                         # => "Symbol > Value > Object"
Proc(Int32).inheritance_chain                    # => "Proc(Int32) > Struct > Value > Object"
Set(String).inheritance_chain                    # => "Set(String) > Struct > Value > Object"
Exception.inheritance_chain                      # => "Exception > Reference > Object"
Class.inheritance_chain                          # => "Class > Value > Object"

# union
alias UnionType = Int32 | Nil | String
UnionType.inheritance_chain                      # => "(Int32 | String | Nil) > Value > Object"

# nilable
Int32?.inheritance_chain                         # => "(Int32 | Nil) > Value > Object"

# pointer
alias Int32Ptr = Int32*
Int32Ptr.inheritance_chain                       # => "Pointer(Int32) > Struct > Value > Object"

# ...

关于ruby - 获取有关 Crystal 中继承链的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889936/

相关文章:

c++ - 如何自动查找大型 C++ 代码中的所有钻石?

html - 如何清除子元素的所有样式?

go - 如何使用反射动态构建结构 slice

java - 如何获取注解上方的注解

ruby-on-rails - rails 2 : test 'link_to' and other view helpers from the rails console?

ruby - 为什么在这个简单的 Active Record 示例中没有保存 id?

Ruby net-ldap 添加用户

ruby - 在 Rails 中使用 uuidtools 生成一个短的 UUID 字符串

java - 当实例来自另一个类时如何访问父类(super class)的实例?

java - 获取数组类型的 Class 对象的最佳方法是什么?