Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix key type for empty NamedTuple be Symbol #10942

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions spec/std/named_tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,18 @@ describe "NamedTuple" do
NamedTuple.new.empty?.should be_true
end

it "does to_a" do
tup = {a: 1, b: 'a'}
tup.to_a.should eq([{:a, 1}, {:b, 'a'}])
describe "to_a" do
caspiano marked this conversation as resolved.
Show resolved Hide resolved
it "creates an array of key-value pairs" do
tup = {a: 1, b: 'a'}
tup.to_a.should eq([{:a, 1}, {:b, 'a'}])
end

it "preserves key type for empty named tuples" do
tup = NamedTuple.new
arr = tup.to_a
arr.should be_empty
arr.should be_a(Array({Symbol, NoReturn}))
end
end

it "does map" do
Expand Down Expand Up @@ -327,10 +336,19 @@ describe "NamedTuple" do
u.should_not eq(v)
end

it "does to_h" do
tup1 = {a: 1, b: "hello"}
hash = tup1.to_h
hash.should eq({:a => 1, :b => "hello"})
describe "to_h" do
it "creates a hash" do
tup1 = {a: 1, b: "hello"}
hash = tup1.to_h
hash.should eq({:a => 1, :b => "hello"})
end

it "creates an empty hash from an empty named tuple" do
tup = NamedTuple.new
hash = tup.to_h
hash.should be_empty
hash.should be_a(Hash(Symbol, NoReturn))
end
end

it "does to_s" do
Expand Down
12 changes: 8 additions & 4 deletions src/named_tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,12 @@ struct NamedTuple
# tuple = {name: "Crystal", year: 2011}
# tuple.to_a # => [{:name, "Crystal"}, {:year, 2011}]
# ```
#
# NOTE: `to_a` on an empty named tuple produces an `Array(Tuple(Symbol, NoReturn))`
def to_a
ary = Array({typeof(first_key_internal), typeof(first_value_internal)}).new(size)
ary = Array({Symbol, typeof(first_value_internal)}).new(size)
each do |key, value|
ary << {key.as(typeof(first_key_internal)), value.as(typeof(first_value_internal))}
ary << {key.as(Symbol), value.as(typeof(first_value_internal))}
caspiano marked this conversation as resolved.
Show resolved Hide resolved
end
ary
end
Expand All @@ -508,9 +510,11 @@ struct NamedTuple
# tuple = {name: "Crystal", year: 2011}
# tuple.to_h # => {:name => "Crystal", :year => 2011}
# ```
#
# NOTE: `to_h` on an empty named tuple produces a `Hash(Symbol, NoReturn)`
def to_h
{% if T.size == 0 %}
{} of NoReturn => NoReturn
{} of Symbol => NoReturn
{% else %}
{
{% for key in T %}
Expand Down Expand Up @@ -582,7 +586,7 @@ struct NamedTuple
{% end %}
end

private def first_key_internal
private def first_key_internal : Symbol
caspiano marked this conversation as resolved.
Show resolved Hide resolved
i = 0
keys[i]
end
Expand Down