-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
a+b=2 should be an error #2032
Comments
You just defined the method
I also think that this behavior can be pretty confusing. I'm not sure how easy it is to get at it, since |
now that i understand it , i kind of like it :-) julia> a+b=5;1+1 |
It seems to me this was closed because it was fixed but not fully (I'm using 0.3.1). julia> a+b=2 [message should maybe say you did NOT redefine? not too important..] julia> 1+2 However, the above fix is not consistent with (implemented more fully): julia> a+b+c=2
[still learning, not sure why +(Any,Any,Any) is treated differently with the fix - I assume since it is because it's another function; not sure why it wasn't fixed - overlooked/not important or likely to be a common mistake?] julia> 1+2+3 julia> 1+2+3+4 julia> a+b+c+d=2
julia> 1+2+3+4 Didn't check all operators, just noting * works but not: julia> a_b_c=2
julia> 1_2_3 [not sure why I get a different error message here, but - is prevented] julia> a-b-c=2 There are probably endless variations..: julia> div(a, x)=2 julia> div(10, 2) This one SHOULD work this way? When is a function just a function that you want to be able to redefine? |
There wasn't any fix – |
Confused then - strike that seems to be a variant of my first bug..: julia> a+b=2 julia> 1+2 |
Also Also all of these syntaxes do not "define functions", they add methods. In every case. With no types specified, the default is |
The following will avoid "inheriting" the standard
|
Why is the three-way + inherited without global? See my first comment.. |
Because all definitions of a given + are part of the same function object. |
While this definitely makes sense, we may question whether supporting this syntax for defining methods is a good idea in practice. Requiring people to write |
What I meant with last comment (note no global +): julia> a+b+c=2
julia> 1+2+3 Doesn't work the same as with (this would require global +):
julia> 1+2 |
That's because with a+b+c=2, you're actually redefining a method in Base, On Tue, Oct 7, 2014 at 5:46 AM, Páll Haraldsson [email protected]
|
In [15]: method_exists((+),(Any,Any,Any))
Out [15]: true
In [16]: method_exists((+),(Any,Any))
Out [16]: false |
@PallHaraldsson the warning is giving you a hint about what's happening. If you look at line 82 of I agree with @nalimilan that infix-syntax method definition for operators is perhaps more confusing than it is useful. |
I didn't understand what is going on at first (infix-syntax method definition). I'm sure I'll remember now, but it doesn't look clear/clean to me quite yet, and it's one more data point, somehow, from someone who's used the language for a while, extended |
discovered by my 10 year old son:
julia> a
a not defined
julia> a+a=2
syntax error: function argument names not unique
julia> a+b=2 # should be an error
julia> a
a not defined
julia> b
b not defined
The text was updated successfully, but these errors were encountered: