-
-
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
Consider using :=
instead of =
for method definitions
#1846
Comments
I appreciate these arguments. If we ever start using We might be able to allow more readable
|
Ok, I see that you might have other plans for the Though method definition is indeed superficially similar to assignment, I do think that there are some very clear differences. Method definition, unlike assignment, defers evaluation of the right hand side. But, to my mind, the main difference is that
Also, assignment can be precompiled, which I am not sure that method definition always can. Indeed, method definitions will influence the compilation of other parts of the code! This distinction might become more important with static compilation. In that sense, |
We do need a in place operator. For any iterative algorithm, there must be some variables need to be reused The storage of myMatrix should be reused instead of allocating new ones and deallocating myMatrix. I am thinking of .= for this. |
We decided not to use := for method definitions. |
Fine. |
My feeling is that assignment
and (method) definition
are really two different things, and the fact that the operator
=
has been overloaded to do both is creating unnecessary confusion and complications. Consider e.g. #1717. If the method definition syntax were changed toinstead (which feels like a sensible alternative), remnants of matlab indexing such as
x(i) = 1
would be able to give much more sensible error messages.This is of course a breaking change of epic proportions, but do I think that it would be worth it in the long run. The two syntaxes for method definition could coexist for a long time to ease the transition.
I think that the main benefits with this change would be increased clarity and syntactic elbow room, and better error messages. A few thoughts about possible benefits:
Currently, assignment syntax
=
serves multiple purposes in julia, depending on the type of the left hand side in the AST:Symbol
==> variable assignment:.
expression ==> field assignment (setfield
):ref
expression ==> indexed assignment (assign
):call
expression ==> method definitionThe last one clearly stands out. It is not always easy to see what constitutes a method call, e.g. the fact that
x+y=z
is a method definition might catch many by surprise.x+y:=z
would leave no doubt that this is a method definition.Method definitions that mirror usage could be made to work consistently. E.g.
I don't know if this would be a preferred style, but it does have the beauty to make explicit in the method definition what kind of operation is being overloaded. Neither the writer nor the reader would have to know that
ref
corresponds to indexing.Generalized assignment, as discussed here could be implemented, if desired.
The way that I see it, the only real downside is that all existing code will have to be updated to accommodate the change. Others will have to weigh that against the benefits.
The text was updated successfully, but these errors were encountered: