-
-
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
make .=
return its right hand side. fixes #25954
#26088
Conversation
Fixes #25954 |
Since the whole point of In what circumstance is returning the RHS, i.e. allocating a temporary that the user presumably chose |
That's a good question. It's hard to say, since there are almost no (maybe actually zero?) examples of code that uses the value of One possible use is Another issue is that e.g. |
Returning the LHS lets you chain the .= assignment with additional in-place functions like sort! If you expect the RHS to be broadcasted, it’s not so strange that it changes shape—this is what broadcasting does. |
It can change type as well as shape. In |
That's true. I still don't see it as a big problem. If someone writes
Defining it to return the RHS seems to fundamentally conflict with the purpose of |
I'm not so sure about that. There are no registered packages with single lines of code with I could get behind an error here. |
@mbauman, in all of the examples you cite, it sounds like returning the LHS is the desired behavior. This makes total sense to me, because using |
I mean, it is that way because that's the way it is. I do agree that this could result in unexpected allocations julia> function f(x)
local a::Any
local b::Int
a = b = x
(a, b)
end
f (generic function with 1 method)
julia> f(2.)
(2.0, 2)
julia> function g(x)
a = Array{Any}(uninitialized, 1)
b = Array{Int}(uninitialized, 1)
@. a = b = x
(a, b)
end
g (generic function with 1 method)
julia> g([2.])
(Any[2], [2]) |
From triage: I'll add a deprecation warning for this (and therefore not change the behavior), since that would be the usual course of action anyway, and then we can decide later whether to change it or turn the warning into an error. |
There are two cases essentially:
In the first case, On the other hand, evaluating the RHS is consistent with the behavior of Overall, I have a hard time seeing valid arguments for returning the LHS versus the RHS. |
If you define: function f!(x)
x .= foo.(x)
end shouldn't it return the LHS ( If you do a function call sort!(x .= x.^2) shouldn't it call These, to me, are more realistic uses of the value of |
I just have a hard time seeing how that's any different from: julia> function f()
local x::Int
x = 2.0
end
f (generic function with 1 method)
julia> f()
2.0 Or: julia> function settwototwo!(x)
x[2] = 2
end
settwototwo! (generic function with 1 method)
julia> settwototwo!([4,3,2])
2 |
Because the whole point of The question is, are there any practical circumstances where the users of Would you be happy if you wrote |
It just means you need to write |
Sure, there is a workaround. There is also a workaround if you want the RHS. That doesn’t address the question of which behavior is more useful. |
That's true, but it's hard to guess what we would find more useful, so we need to have simple rules like "assignments always return the RHS". Anyway, I believe this is used quite rarely, so I'll proceed with just deprecating it. That way there's no final decision yet. |
95b57e9
to
c3042f5
Compare
c3042f5
to
f4a2bc8
Compare
I'm getting false positives in #24368 with code like this: $ ./julia --depwarn=error -q
julia> module TestFoo
x .= y
nothing
end
ERROR: syntax: Deprecated syntax `using the value of `.=``. |
…gnment * master: Make stdlib tests runnable standalone. (#26242) fix unary-related parsing regressions caused by #26154 (#26239) Formatting changes to new SSA IR devdocs [ci skip] use medium code model on PPC `retry` should support the check function proposed in the docstring. (#26138) mention axes in docs instead of size (#26233) exclude more CI files from source distro (#25906) Describe three-valued logic in docstrings deprecate using the value of `.=`. fixes #25954 (#26088) backport change to make CodegenPrepare work with isNoopAddrSpaceCast optimize the python version of abs2 in the microbenchmarks (#26223) Add notes for package maintainers (#25912) typo Fix broken links to functions in the manual (#26171) [NewOptimizer] Track inlining info Begin work on new optimizer framework add patch to make GC address spaces work on PPC also backport sover patch to LLVM 4.0
This is a really simple, sub-optimal solution to try to get the right behavior in place quickly. The hack is to lower
.=
both ways (the usual broadcast fusion way, and the version that saves the RHS in a temporary variable), and select one at the point where we know whether the expression's value is used.If we want, the same mechanism could instead be used to give an error for using the value of
.=
, if we'd rather not decide now.