Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 22 additions & 6 deletions spec/std/tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,29 @@ describe "Tuple" do
({1, 2} === nil).should be_false
end

it "does to_a" do
ary = {1, 'a', true}.to_a
ary.should eq([1, 'a', true])
ary.size.should eq(3)
describe "#to_a" do
describe "without block" do
it "basic" do
ary = {1, 'a', true}.to_a
ary.should eq([1, 'a', true])
ary.size.should eq(3)
end

it "empty" do
ary = Tuple.new.to_a
ary.size.should eq(0)
end
end

ary = Tuple.new.to_a
ary.size.should eq(0)
describe "with block" do
it "basic" do
{-1, -2, -3}.to_a(&.abs).should eq [1, 2, 3]
end

it "different type" do
{1, 2, true}.to_a(&.to_s).should eq ["1", "2", "true"]
end
end
end

# Tuple#to_static_array don't compile on aarch64-darwin and
Expand Down
10 changes: 3 additions & 7 deletions src/tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,16 @@ struct Tuple
# {1, 2, 3, 4, 5}.to_a # => [1, 2, 3, 4, 5]
# ```
def to_a : Array(Union(*T))
{% if compare_versions(Crystal::VERSION, "1.1.0") < 0 %}
to_a(&.itself.as(Union(*T)))
{% else %}
to_a(&.itself)
{% end %}
Comment on lines -548 to -552
Copy link
Member Author

@straight-shoota straight-shoota Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, the explicit cast here is always necessary, even after 1.1.0 because the compiler couldn't figure out the output type of &.itself.

to_a(&.as(Union(*T)))
end

# Returns an `Array` with the results of running *block* against each element of the tuple.
#
# ```
# {1, 2, 3, 4, 5}).to_a { |i| i * 2 } # => [2, 4, 6, 8, 10]
# ```
def to_a(& : Union(*T) -> _)
Array(Union(*T)).build(size) do |buffer|
def to_a(& : Union(*T) -> U) forall U
Array(U).build(size) do |buffer|
{% for i in 0...T.size %}
buffer[{{i}}] = yield self[{{i}}]
{% end %}
Expand Down