-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: add "++" operator #11686
RFC: add "++" operator #11686
Conversation
I like this operator. For me, the toughest question is how this relates to |
Also, I didn't know how to make |
Hooray code! Thanks for starting the ball rolling here. It's so much easier to discuss implementations instead of going around in circles of spilled ink. Given the timeline @IainNZ and I were discussing in #11030 (comment), maybe this should be pared down to simply be the addition of the parsed operator (with no builtin meaning at all). That way this can be developed over the course of 0.4 in a package to experiment with things like
That's a bit of a harder problem. Right now |
The test failure is coming from a parsing test of |
@mbauman Thanks again for taking time to point out some of the internals. I won't bother following up on the multi-argument parsing then. As an additional note, I think if it is legitimate to do |
@@ -108,6 +108,9 @@ end | |||
const .≤ = .<= | |||
const .≠ = .!= | |||
|
|||
#For now, no generic behavior for ++ | |||
++(x,y) = error("++ only defined for strings") | |||
|
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.
It would be better for this to be a no method error.
Ref #9532, past issue on parsing of This would be hypothetically breaking some not-particularly-legible code, but I think I'd be in favor of allowing it to be parsed as an operator for now (would we need to make |
"I was quite surprised issue #11030 reach 160+ comments", yes.. I'm not trying to do that here.. See my latest comment there about: "Also, I didn't know how to make x ++ y ++ z parse to ++(x,y,z) so it creates ++(++(x,y), z)", parsing to ++(x,y,z) would be good but not if it results in two operations. If "not allowing any other generic use. [..] This will break code if anyone was doing something like x++1 (as x + (+1)" your operator might be better, because it disallows non-string types, or worse depending how you look at it.. This already works (or with println): julia> string(1, "10", 1) It's grate that you're learning Julia and trying to help. I'm not sure we should add this if string or that function renamed to cat or c function might be better, then we have to get rid of two operators.. if that ended up as the recommended way. Would the ++ ever be useful for other things, such as, in C++? [Another operator (already taken..) || is good.. used by SQL, would not have had the numbers problem or could cat them.. It's only defined for booleans, not sure it's a good idea to define it for this for all non-booleans.. Maybe it would work? As the result of || is also bool and you would get an error down the line. Immediately if in a condition, but this is probably a very bad idea as errors should be localized.] |
@StefanKarpinski I removed the generic fallback method so any call that isn't using strings will throw a MethodError.
I think if people really wanted an increment and/or decrement unary operator, we would have seen more requests on the mailing list, etc. It's also pretty tough to make a good case that |
I really think that this should not be defined with It also means that you couldn't fix things so that b"abcde" ++ b"fghij" would work as expected. |
@ScottPJones I think you misread the changes here. This doesn't call However, it is looking like people are more interested in allowing the operator parsing without even the string concat logic. I personally am somewhat skeptical about adding an operator without giving it some sort of default meaning (why is it being added then?) but will leave it up to the experts. |
Here is a little bit of what I'd been working on (but all my suggestions for a specific operator, including ⊕{T <: Union{AbstractString,Char}}(a::T, b::T...) = string(a, b...)
⊕(a::Char, b::Char...) = string(a, b...)
⊕{T <: Union{UInt8, UInt16, UInt32, Char}}(a::Union{T,AbstractVector{T}}, b::Union{T,AbstractVector{T}}...) = vcat(a, b...) julia> "foo"⊕"b"
"foob"
julia> "foo"⊕'c'
"fooc"
julia> b"foo"⊕b"bar"
6-element Array{UInt8,1}:
0x66
0x6f
0x6f
0x62
0x61
0x72 |
@phobon Yes, I saw that, that's why I said "in the general case". I was afraid that you'd change it to use |
@phobon I tried exactly that, and had my PR #11102 peremptorily closed 😞 |
Let's not get distracted by the general case. Part of the beauty in this PR is its minimalism. Once the operator can be parsed, a package can drive development of the more complicated cases. As it stands, this PR entails two fairly straightforward decisions:
|
@ScottPJones I think those methods all look quite reasonable. However, I don't think this is the place to discuss them since that amount of logic will definitely not be in this PR, which is just about whether the |
@phobon That's fine, although I would suggest the change I had in the first method, that allows |
@mbauman That's why I'd been trying, in #11102, to find some character(s), such as
Remember, I did submit code also, for PR #11102... |
For those confused, @ScottPJones is meaning to link to #11102. For now, I won't propose any further extension beyond what |
BTW, very good work @phobon, I'll be incredibly happy to see this get in... |
Ok, I removed all exports and concat meaning so this PR is now just about the parsing. This is already sufficient to experiment with various |
I downloaded and grep'd through all the package code for the string "++" and found it used in two places. Both of them looked to be mistaken translations from C/C++ where the increment operator was not rewritten. I'll go ahead and submit PRs to those packages regardless of what happens here. Given the imminent |
👍 This is a nice adition to 0.4 |
I would prefer not to have such a major syntax change this late into the 0.4 release cycle, and instead have it for the 0.5 cycle. |
This is a pretty minor parsing adjustment and leaves most of the question of what to do with this operator for packages to play with, so I don't think of it as that disruptive to do now? The other recent syntax change in #12285 was quite a bit more disruptive than this would be. |
@JeffBezanson, I've added the n-ary parsing behavior. I did what was probably very naive changes to make it work but it seems to be working ok and not conflicting with the chain parsing of |
Looks good, thanks! |
I wonder if immutable ConcatString{N,S<:String} <: String
strings::NTuple{N,S}
end It's probably pretty common to know how many things have been concatenated up to a point. If you're constructing one of these in a loop you should probably be using an |
Yes, we might want to experiment with some iovec-like things like that. Would you still be in favor of n-ary parsing of |
Yes, n-ary parsing strikes me as better in any case. |
🎆 👏 |
Quoting myself from #11686 (comment)
|
I don't think so. |
Shouldn't this go on NEWS.md? |
Not NEWS-worthy IMO, since we're not actually adding any functionality, just parsing it as an operator. |
Well, given the vast amount of discussion that surrounded this issue, I thought people would be interested nevertheless. Besides, it is a language change, albeit admittedly a small one. |
I wonder if |
I'm just saying, lots of people will be interested in knowing this news, regardless of how significant the developers consider this change to be. Purposely choosing to be quiet about this change is IMO a disservice to all the people who engaged in those conversations. |
It's fine to announce it and include a list of new operators. Please feel free to make a PR, @waldyrious. |
Will do :) What other operators should I include? #12472? Any others I'm not aware of? |
Alternatively I can add only |
I think you can mention |
Ok. I also realized that |
Sure, list them all – for some reason I thought more operators were added in 0.4 but maybe it was 0.3. |
I think more unicode operators are parsed on 0.4 than on 0.3 but not very many of them have pre-assigned meanings. |
mention ++, per discussion at #11686
If I'm reading |
I was quite surprised issue #11030 reach 160+ comments and no one had created a PR for introducing
++
, as it seemed to have very broad support. It was a pleasant surprise how easy it was to add. Serious thanks to @mbauman for providing the hint to get this functionality that an outsider like me would otherwise have no idea about.Speaking of being an outsider, I tried to be as non-controversial as possible, by just replicating the current*
concat method and not allowing any other generic use. I was motivated to make this PR because I really liked the idea of a generic sequence concat operator but others can work out the details for that.This will break code if anyone was doing something like
x++1
(asx + (+1)
) which is pretty unlikely but possible.I guess the long-term plan would be to decide if
++
should be added to 0.4, and then if so, the whole debacle of deprecating*
would be discussed/argued/executed in 0.5-dev? I'm looking forward to watching the comments with my popcorn...