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

ope*_*sas 2 ruby reflection inheritance metaprogramming crystal-lang

出于好奇心并且稍微了解了Crystal的一般结构,我一直在寻找一些反射功能,这些功能可以让我更好地理解继承链是如何构建的.

我在想像ruby的超类,祖先included_modules方法.

Crystal语言中是否有这样的东西?

此外,有一些图表可以向我展示大局是有用的.

Vit*_*upt 5

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

宏是在编译时接收AST节点并生成粘贴到程序中的代码的方法.

Crystal已经实现了superclass并且ancestors在编译时返回结果.所以你可以这样做:

pp({{ MyClass.superclass }})
pp({{ MyClass.ancestors }})
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

然后你可以做检查:

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 # => []
Run Code Online (Sandbox Code Playgroud)

如果你走得更远:

C.superclass                                                    # => A
C.superclass.try &.superclass                                   # => Reference
C.superclass.try &.superclass.try &.superclass                  # => Object
C.superclass.try &.superclass.try &.superclass.try &.superclass # => nil
Run Code Online (Sandbox Code Playgroud)

现在使用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"

# ...
Run Code Online (Sandbox Code Playgroud)