3
我正在嘗試爲接收散列作爲參數的類創建初始化程序。散列是一個{String => Type}散列,並且可以嵌套。鍵入別名和散列作爲方法參數
#file: types.cr
class Types
alias Type = Nil |
Bool |
Int32 |
Int64 |
Float64 |
String |
Array(Type) |
Hash(String, Type)
def initialize(@input : Type)
end
end
input = {"a" => {"b" => {"c" => {"c1" => 1, "c2" => 2, "c3" => true}}}}
s = Types.new(input)
這裏運行上面的代碼時,我得到的錯誤:
$ crystal types.cr
Error in types.cr:16: instantiating 'Types:Class#new(Hash(String, Hash(String, Hash(String, Hash(String, Bool | Int32)))))'
s = Types.new(input)
^~~
in types.cr:11: instance variable '@input' of Types must be Types::Type, not Hash(String, Hash(String, Hash(String, Hash(String, Bool | Int32))))
def initialize(@input : Type)
^~~~~~
這可能與水晶運行此代碼時,我得到一個錯誤?我應該如何處理這個問題?
謝謝!