我試圖編寫一個實用程序庫,它試圖調用任意對象類型的方法。在Ruby中,我會做一些事情,如:未知類型的方法參數
def foo(object)
object.public_send(:bar)
rescue NoMethodError
raise "Method not defined on object"
end
foo(instance_of_my_arbitrary_class)
我不知道如何在水晶做到這一點,因爲我們的類型未知,所以我得到的Can't infer the type of instance variable 'object'
編譯錯誤。
如何在不知道要傳遞的對象類型的情況下完成此操作?
您是否正在設計一個抽象類? –
@MarkThomas也許?只是學習,讓我看看抽象類。 –
這是Crystal中的[已知限制](https://github.com/crystal-lang/crystal/issues/2733)。可能你可以使用[泛型]實現這個(https://crystal-lang.org/docs/syntax_and_semantics/generics.html) –