1
據我瞭解的類型聲明就像kind_of
...定義實例方法與「宏觀繼承」水晶0.21.1
「所以類型不嚴格的類型命名,而是像一個
is_a?
或kind_of?
。「https://github.com/crystal-lang/crystal/issues/4232
所以我認爲這部分代碼有效。
但是當我使用宏來定義一個繼承類的實例方法時,錯誤消息會抱怨從錯誤的對象中缺少的方法。
class Base
macro inherited
def name
"Joe Smith"
end
end
end
class Context < Base; end
class Render
def initialize(@inner_context : Base); end
def display
name
end
forward_missing_to inner_context
private property inner_context
end
puts Render.new(Context.new).display
,輸出是:
Error in line 23: instantiating 'Render#display()'
in line 15: instantiating 'name()'
in macro 'forward_missing_to' /usr/lib/crystal/object.cr:1132, line 1:
> 1. macro method_missing(call)
2. inner_context.{{call}}
3. end
4.
expanding macro
in macro 'method_missing' expanded macro: forward_missing_to:1, line 1:
> 1. inner_context.name
2.
undefined method 'name' for Base (compile-time type is Base+)
缺少什麼我在這裏?
哇!這樣可行!非常感謝! –