Creating domain-specific languages in Julia using macros
julialang.org
julialang.org
There's a bunch of his videos in the Julia channel on Youtube.
How many times does this mistake have to be repeated in various languages? It'll just be a matter of time before people are banging on for a strict mode, and rightfully so.
At least the default scope in Julia is local. :/
If you're gonna do lexical scoping or some variation of, defaulting to the current scope makes the most sense. Defaulting to global scope is what causes the issue, not choice of keywords.
If you're creating a closure, that's inherent part of the enclosing scope. Just like variable reuse is discouraged for clarity, so it should in closures.
I think declaring a global variable deep in scope sounds like recipe for hell. I think declaring a global with a name as short as you'd use in function parameters is a mistake. I don't see how it'd be a problem if you follow practices that encourage maximum clarity.
If I could see an example, it'd make it easier to understand your point - I haven't seen the problem you describe so it's difficult for me to visualise it.
def f():
def g():
nonlocal x # change this line
x = 3
print("x == {!r}".format(x))
x = 2
g()
print("x == {!r}".format(x))
x = 1
f()
print("x == {!r}".format(x))
It will print (because g reassigns the x in f): x == 3
x == 3
x == 1
and if you change "nonlocal" to "global", g will reassign the global x, and you get: x == 3
x == 2
x == 3
and if you remove the line entirely, the assignments will be interpreted as declaring new variables in each scope, and you get : x == 3
x == 2
x == 1From the perspective of language design, attempts to improve upon explicitly declared block-scoped lexical variables generally fail.
I'm not passionately for implicit declarations. I just don't see it as a make or break, stupid idea that the OP made it out to be.
If I had a choice, I would prefer explicit, with compiler errors when not declared up scope as opposed to implicit declaration in the global scope.
I've used JavaScript and PHP in the past (and even enjoyed it!), never mind all the misfeatures of said languages. What bugs me though is that language designers keep making the same mistakes over and over again...
To create a closure, you must use "use" and specify the variables to close over. To use globals, you have to say "I want this to be from the global scope or make a global otherwise".
Leads to a lot of verbose code and cripples ability to really use FP ideas.