-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Implement some methods on Literal::Array #143
Conversation
lib/literal/array.rb
Outdated
when Literal::Array | ||
# Do nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move line 86 in here?
Also, I think above this, we can handle the case that we’re adding an another Literal::Array of the same type. Should be able to do when @__generic__
(see above).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joeldrapper Yes, it ends up looking either like:
def +(other)
case other
when ::Array
other_literal = @__generic__.new(*other)
Literal::Array.new(@__value__ + other_literal.__value__, type: @__type__)
when Literal::Array
Literal::Array.new(@__value__ + other.__value__, type: @__type__)
else
raise ArgumentError.new("Cannot perform + with #{other.class.name}.")
end
end
or
def +(other)
case other
when ::Array
values = @__value__ + @__generic__.new(*other).__value__
when Literal::Array
values = @__value__ + other.__value__
else
raise ArgumentError, "Cannot perform `+` with #{other.class.name}."
end
Literal::Array.new(values, type: @__type__)
end
I'm partial to the second, but let me know which style you prefer.
def +(other) | ||
case other | ||
when ::Array | ||
values = @__value__ + @__generic__.new(*other).__value__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably check other against the collection type and then use __with__
to return a Literal::Array. I believe this will return an Array at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates a temporary variable values
(perhaps poorly named, too close to __values__
) that is used to initialize a new Literal::Array
on line 101. So the type checking is deferred to the instantiation of the new Literal::Array
at the end of the method.
I think my error here is that I'm creating a new Literal::Array twice. This should work as just:
values = @__value__ + other
And the return value could actually be:
@__generic__.new(*values)
So the whole method would be
def +(other)
case other
when ::Array
values = @__value__ + other
when Literal::Array
values = @__value__ + other.__value__
else
raise ArgumentError.new("Cannot perform `+` with #{other.class.name}.")
end
@__generic__.new(*values)
end
Does this approach make sense; should we be checking types inside the case...when
?
I’m going to merge this and then take a look at |
Implementation and tests for:
Literal::Array#*
Literal::Array#+
Literal::Array#-
Closes #152
Closes #151
Closes #153
Closes #156
Closes #160
Closes #162
Closes #184