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

Implement Char#*(times) operator #3860

Closed
wants to merge 2 commits into from

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Jan 8, 2017

Comes useful when in need to repeat just one Char without reverting to String.

@Sija Sija changed the title Implement Char#* operator Implement Char#*(times) operator Jan 8, 2017
def *(times : Int)
raise ArgumentError.new "negative argument" if times < 0

if times == 0 || bytesize == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

bytesize == 0 is impossible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See my previous comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RX14 I was still thinking of String, my bad - thanks for pointing that out! just fix-ed it

# ```
# '-' * 10 # => "----------"
# ```
def *(times : Int)
Copy link
Contributor

Choose a reason for hiding this comment

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

Call times count internally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implementation is taken straight from String#*

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to change both here and at String#*

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it in both, String#* and Char#* methods.

@lbguilherme
Copy link
Contributor

LGTM

# ```
# '-' * 10 # => "----------"
# ```
def *(times count : Int)
Copy link
Member

Choose a reason for hiding this comment

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

@Sija why named arguments in an infix operator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for joining late :-), but it's hard to imagine someone will write "aString".*(times=3).
I am ok with either name, but I see no point with the named argument in an infix operator.

Unless there is a reason I would drop that. Again any internal name is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point, although I was thinking about the docs, where the argument name is shown.

@@ -2149,19 +2149,19 @@ class String
# "Developers! " * 4
# # => "Developers! Developers! Developers! Developers!"
# ```
def *(times : Int)
raise ArgumentError.new "negative argument" if times < 0
def *(times count : Int)
Copy link
Member

Choose a reason for hiding this comment

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

ditto.

And in this case, this change is just a param rename. I get the consistency, but, is there any reason to prefer times over count in both cases?

@asterite
Copy link
Member

Sorry, I'll have to intervene and say no to this. What's the use case for this?

Plus, doing 'a' * 1 # => "a" feels strange. And then, chars and integers are usually intermixed, and we definitely don't want to suddenly allow multiplying chars by integers with a string as a result. String#* already allocates memory, so if you have a single char and need it repeated times (and this something that will probably never happen) then convert it to a String first.

Finally:

'a' + 1 # => b
'a' * 2 # => "aa"

Doesn't make sense at all.

Plus the code for multiplying a Char and String is almost identical, I wouldn't like to have that in the codebase.

@asterite asterite closed this Jan 11, 2017
@Sija
Copy link
Contributor Author

Sija commented Jan 12, 2017

@asterite Fair 'nuff, I was thinking too about intermixing chars and integers which doesn't makes much sense. Also, we need to allocate String anyway so performance gain is minute if any. Thanks for looking into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants