You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The dig methods of YAML::Any does not accept a string key so the following code:
require"yaml"
example_yaml ="---test: example_1: 1 example_2: 2"
data =YAML.parse(example_yaml).as_h
puts data.dig("test", "example_1") # => Error: no overload matches 'Hash(YAML::Any, YAML::Any)#dig' with types String, String
Raises instead of the expected output of 1.
While I'm not entirely sure if this is a bug, it does feel incredibly awkward to have to wrap a YAML::Any around the arguments before the compiler accepts #dig
I think I meant I was thinking that Hash#dig is defined as def dig(key : K, *subkeys), notice the K type restriction. Whereas YAML::Any#dig is implemented as def dig(index_or_key, *subkeys) : YAML::Any without any type restrictions. But I know the latter will end up using the definition of the former internally so not sure sure how it avoids the compiler error when the type of the hash should be essentially the same?
Yeah, not a bug. You're calling to_h so you get a raw Hash instead of a YAML::Any wrapper around it. That hash expects YAML::Any keys for its #dig method. You probably want to skip to_h and use YAML::Any#dig instead.
@Blacksmoke16 Regarding the type restriction, there are some general inconsistencies. Hash(K, V)#[](key) has no type restriction either, for example (ref #8893).
Reacted by Sijawusz Pur Rahnama and George Dietrich
Activity
Blacksmoke16 commentedon Feb 19, 2024
I think your problem is the
.to_hcall afterYAML.parse, if you remove that it works as expected, otherwise you're using theHash#digversion.EDIT: Looks like that works because the
YAML::Anyversion is less restrictive in regards to what you can pass it? 🤷straight-shoota commentedon Apr 4, 2024
Is there anything left to do here?
@Blacksmoke16 could you clarify this comment:
Blacksmoke16 commentedon Apr 4, 2024
I think I meant I was thinking that
Hash#digis defined asdef dig(key : K, *subkeys), notice theKtype restriction. WhereasYAML::Any#digis implemented asdef dig(index_or_key, *subkeys) : YAML::Anywithout any type restrictions. But I know the latter will end up using the definition of the former internally so not sure sure how it avoids the compiler error when the type of the hash should be essentially the same?syeopite commentedon Apr 4, 2024
So not a bug?
straight-shoota commentedon Apr 4, 2024
Yeah, not a bug. You're calling
to_hso you get a rawHashinstead of aYAML::Anywrapper around it. That hash expectsYAML::Anykeys for its#digmethod. You probably want to skipto_hand useYAML::Any#diginstead.straight-shoota commentedon Apr 4, 2024
@Blacksmoke16 Regarding the type restriction, there are some general inconsistencies.
Hash(K, V)#[](key)has no type restriction either, for example (ref #8893).