Skip to content

Support empty hash return type when key => value defined #16

@maedi

Description

@maedi

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]

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions