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

Ensure Enumerable#to_a and Enumerable#tally properly retain return type of T #14447

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 29 additions & 2 deletions spec/std/enumerable_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
require "spec"
require "spec/helpers/iterate"

module SomeInterface; end

private record One do
include SomeInterface
end

private record Two do
include SomeInterface
end

private struct InterfaceEnumerable
include Enumerable(SomeInterface)

def each(&)
yield One.new
yield Two.new
end
end

private class SpecEnumerable
include Enumerable(Int32)

Expand Down Expand Up @@ -132,13 +151,17 @@ describe "Enumerable" do
end

describe "to_a" do
context "with a block" do
it "with a block" do
SpecEnumerable.new.to_a { |e| e*2 }.should eq [2, 4, 6]
end

context "without a block" do
it "without a block" do
SpecEnumerable.new.to_a.should eq [1, 2, 3]
end

it "without a block of an interface type" do
InterfaceEnumerable.new.to_a.should eq [One.new, Two.new]
end
end

describe "#to_set" do
Expand Down Expand Up @@ -1456,6 +1479,10 @@ describe "Enumerable" do
{'c' => 1, 'r' => 2, 'y' => 2, 's' => 1, 't' => 1, 'a' => 1, 'l' => 1, 'u' => 1, 'b' => 1}
)
end

it "tallies an interface type" do
InterfaceEnumerable.new.tally.should eq({One.new => 1, Two.new => 1})
end
end
end

Expand Down
33 changes: 33 additions & 0 deletions spec/std/indexable_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
require "spec"
require "spec/helpers/iterate"

module SomeInterface; end

private record One do
include SomeInterface
end

private record Two do
include SomeInterface
end

private struct InterfaceIndexable
include Indexable(SomeInterface)

def size
2
end

def unsafe_fetch(index : Int) : SomeInterface
case index
when 0 then One.new
when 1 then Two.new
else
raise ""
end
end
end

private class SafeIndexable
include Indexable(Int32)

Expand Down Expand Up @@ -818,4 +845,10 @@ describe Indexable do
it_iterates "#each_repeated_combination", [[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 2, 2], [1, 2, 2, 2], [2, 2, 2, 2]], SafeIndexable.new(2, 1).each_repeated_combination(4)
end
end

describe "#to_a" do
it "without a block of an interface type" do
InterfaceIndexable.new.to_a.should eq [One.new, Two.new]
end
end
end
6 changes: 4 additions & 2 deletions src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ module Enumerable(T)
# ["a", "b", "c", "b"].tally # => {"a"=>1, "b"=>2, "c"=>1}
# ```
def tally : Hash(T, Int32)
tally_by(&.itself)
tally_by(Hash(T, Int32).new, &.itself)
end

# Tallies the collection. Accepts a *hash* to count occurrences.
Expand All @@ -1994,7 +1994,9 @@ module Enumerable(T)
# (1..5).to_a # => [1, 2, 3, 4, 5]
# ```
def to_a : Array(T)
to_a(&.itself)
ary = [] of T
each { |e| ary << e }
ary
end

# Returns an `Array` with the results of running *block* against each element of the collection.
Expand Down
11 changes: 11 additions & 0 deletions src/indexable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,17 @@ module Indexable(T)
end
end

# Returns an `Array` with all the elements in the collection.
#
# ```
# {1, 2, 3}.to_a # => [1, 2, 3]
# ```
def to_a : Array(T)
ary = Array(T).new(size)
each { |e| ary << e }
ary
end

# Returns an `Array` with the results of running *block* against each element of the collection.
#
# ```
Expand Down
Loading