When I do:
type_accessor nodes: Hash[String => TrieNode] | nil
And nodes returns an empty hash {}, I get:
LowType::ReturnTypeError: Invalid return type '{}' for method 'nodes'. Valid types: '{String => Rain::TrieNode} | nil
This is because the empty hash doesn't have the key => value that LowType is looking for... it's an empty hash {} and an empty hash is obviously not nil, even though it behaves like a nilable value when coupled with my_hash['missing key'] # => nil.
The current workaround is to use a "catch all" Hash to validate the empty hash return value as true:
type_accessor nodes: Hash[String => TrieNode] | Hash
But ideally I think a "nilable hash" would best be represented as:
# Option 1
type_accessor nodes: Hash[String => TrieNode] | {}
# Option 2
type_accessor nodes: Hash[String => TrieNode | nil]
When I do:
And
nodesreturns an empty hash{}, I get:This is because the empty hash doesn't have the
key => valuethat LowType is looking for... it's an empty hash{}and an empty hash is obviously notnil, even though it behaves like a nilable value when coupled withmy_hash['missing key'] # => nil.The current workaround is to use a "catch all"
Hashto validate the empty hash return value as true:But ideally I think a "nilable hash" would best be represented as: