水晶郎:class对象的method_missing吗?

时间:2018-08-13 08:24:47

标签: crystal-lang method-missing

像这样吗?

class A
    module ClassMethods
        macro method_missing(call)
            {% p call %}
        end
    end
    extend ClassMethods
end
A.a

代码结果为Error : undefined method 'a' for A:Class。看来method_missing不适用于类对象。

该问题的原因是我使用current类方法实现了一个类,以获取当前上下文的实例。为了简化起见,我希望像forward_missing_to这样的人用MyClass.current.xxx代替MyClass.xxx,因此根据method_missing的实现,需要forward_missing_to

我还发现,delegate适用于使用上述方法的类方法。因此,实际上我的实际问题已经解决,但是我对以下问题仍然很感兴趣:对于类对象,method_missing是不可能的吗?

0 个答案:

没有答案