You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In porting code from MATLAB, this one caught me by surprise. Would it be possible (worthwhile) to generate a warning if an inner anonymous function shadows a local array (variable)? The scope of the length(x) call also surprised me, although I can't think of an example where this would matter in real code.
In MATLAB:
x = zeros(10,1)
for i =1:length(x)
x(i) =1;
end
In Julia:
julia> x=zeros(10,1)
julia>for i =1:length(x)
x(i) =1end
in anonymous: x not defined
in anonymous at no file
julia>for i =1:10x(i) =1end
julia> x==zeros(10,1)
true
julia>for i =1:10
x[i] =1end
julia> x==ones(10,1)
true
The text was updated successfully, but these errors were encountered:
@JeffBezanson: I take it that the unexpected part is that the function definition creates a new binding x instead of refusing to add a method to the old binding?
I've gradually been feeling more and more that method definitions should probably not be done with =, since it's too easy to confuse them with assignment. (In the case above, and others.) I think that
f(x) := x^2
would make a nice alternate syntax.
Of course, that would be a breaking change of epic proportions. The old syntax could be kept alongside for a pretty long time to ease the transition. I do think that it would allow julia to be more consistent, and possibly open up for more powerful syntax by making the distinction between assignment and method definition cleaner. As a toy example, consider
(x::MyType)[i::Int] := ...
to define a ref method for MyType. (That doesn't generalize to assign, though... )
In porting code from MATLAB, this one caught me by surprise. Would it be possible (worthwhile) to generate a warning if an inner anonymous function shadows a local array (variable)? The scope of the length(x) call also surprised me, although I can't think of an example where this would matter in real code.
In MATLAB:
In Julia:
The text was updated successfully, but these errors were encountered: