3
パラメータとしてHashを受け取るClassの初期化子を作成しようとしています。ハッシュは{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)
^~~~~~
はクリスタルと、これは可能ですか?私はこれにどのようにアプローチすべきですか?
ありがとうございます!