Use a module to encapsulate your functions. See 1.0 annoyances and Matlab comparison - #140 by PetrKryslUCSD.
module m
local c = 0;
local function d(c2, c3 = "?")
println(c2, "1"); end;
function d(c)
println(c, "2"); end;
d(c);
> 0, 1 ? ## or conflict?
end;
m.d(1);
> 1, 2 ## ?
@PetrKryslUCSD - there are ways around it, but for me, coming from Cpp and Matlab, this is just an (extremely) undesired feature on Julia. Something to be aware of and a source of potential bugs in my code.
Just like the fact that arrays are always passed by ref to functions, as in:
function change_arr(arr_in)
arr_in[2] = 6;
return;
end
arr_in = zeros(Int32, 1, 2);
arr_in[1] = 5;
println("before call - " * string(arr_in));
change_arr(arr_in);
println("after call - " * string(arr_in));
output:
before call - Int32[5 0]
after call - Int32[5 6]
Well yes, it can be an unpleasant surprise if the writer of the function did not indicate that the arguments were getting changed inside. On the other hand, the benefits in terms of performance are tremendous.
One can always screw it up with variables defined outside of the scope of the functions but still within the module. My point was that the module would protect the function from variables defined outside of the module. (The variables defined inside the module are also a little bit easier to control for these sorts of conflicts.)
Arguments are always passed by value in julia. This is nice because the is no need for the constant fear of redundant copies like in c++.
Agreed and point noted
That was just a little experiment with module local scopes, itās probably a bit tricky to make use of though - especially the local function
Itās quite easy to declare globals accidentally too, often you just need a top-level var with local scope
I think you have been misinformed. Matlab too has this (see Nested Functions - MATLAB & Simulink - MathWorks Nordic) The only reason you donāt easily see it is that you cannot define ordinary functions on the command line (an anonymous function will show you though.)
Javascript also has it, I think in fact it is very common.
In C++ you cannot define nested functions, apparently, otherwise they would probably have it too.
Shouldnāt the same rule apply for similar
then?
To me the fact itās allocating uninitiliaizieid [*] memory is as implicit here as with Array
.
[*] - I clearly donāt like the wording
Edit: Woops, didnāt see this thread went stale. Sorry for bumping.