Skip to content

Commit

Permalink
Align Reflect's method and related comments with the class' purpose
Browse files Browse the repository at this point in the history
With this PR, Reflect is not used to determine the first type of a union type.
This commit aligns Reflect's method and related comments with the class'
purpose.
  • Loading branch information
rvprasad committed Dec 31, 2024
1 parent 51afe63 commit 1389213
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/enumerable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ module Enumerable(T)
end

private def additive_identity(reflect)
type = reflect.first
type = reflect.type
if type.responds_to? :additive_identity
type.additive_identity
else
Expand Down Expand Up @@ -1850,15 +1850,15 @@ module Enumerable(T)
# ```
#
# This method calls `.multiplicative_identity` on the element type to determine the
# type of the sum value.
# type of the product value.
#
# If the collection is empty, returns `multiplicative_identity`.
#
# ```
# ([] of Int32).product # => 1
# ```
def product
product Reflect(T).first.multiplicative_identity
product Reflect(T).type.multiplicative_identity
end

# Multiplies *initial* and all the elements in the collection
Expand Down Expand Up @@ -1901,7 +1901,7 @@ module Enumerable(T)
# ([] of Int32).product { |x| x + 1 } # => 1
# ```
def product(& : T -> _)
product(Reflect(typeof(yield Enumerable.element_type(self))).first.multiplicative_identity) do |value|
product(Reflect(typeof(yield Enumerable.element_type(self))).type.multiplicative_identity) do |value|
yield value
end
end
Expand Down Expand Up @@ -2293,10 +2293,9 @@ module Enumerable(T)

# :nodoc:
private struct Reflect(X)
# For now it's just a way to implement `Enumerable#sum` in a way that the
# initial value given to it has the type of the first type in the union,
# if the type is a union.
def self.first
# For now, Reflect is used to reject union types in `#sum()` and
# `#product()` methods.
def self.type
{% if X.union? %}
{{
raise("`Enumerable#sum()` and `#product()` do not support Union " +
Expand Down

0 comments on commit 1389213

Please sign in to comment.