3 ms·
Way to miss the issue. I was not suggesting ":=" for global variables at all. I was suggesting ":=" as a replacement for the nonlocal keyword that Python 3 ha
by the_mitsuhiko 15y ago
Way to miss the issue. I was not suggesting ":=" for global variables at all. I was suggesting ":=" as a replacement for the nonlocal keyword that Python 3 has to solve the issue I was demonstrating.
- showell30 15y agoNope, I get it, you were suggesting ":=" as a replacement for the "nonlocal" keyword in Python. I'm not sure how my comments demonstrate any lack of reading comprehension. All I said about ":=" and "nonlocal" is that they are overly complicated once you accept that top-level variables can have wide scope. Obviously, plenty of folks managed to write bug-free code in Python before "nonlocal" was invented. I'm not saying it's a bad idea, but you can avoid bugs without it.
- the_mitsuhiko 15y ago> Obviously, plenty of folks managed to write bug-free code in Python before "nonlocal" was invented. Python has the inverse behavior of CoffeeScript. So that was never an issue.
- showell30 15y agoWell, fine, but Python2 had the inverse problem--you couldn't mutate top-level variables without awkward "global" statements.
- notSorella 15y agoThat's a whole different matter, which can't make you shoot yourself in the foot without knowing it for any non-trivial code. CoffeeScript's scoping forces you to always keep track of whatever is enclosing the current scope ALL THE WAY TO THE TOP. This is way too much when your function doesn't need to access outer variables (which should be the minority of the cases). So, problem is, you either make all your functions have non-free variables (but Jashkenas seems to dislike functions shadowing outer variables too, which is just... overtly weird), or you keep track of all variables above the current scope. The former is not too unreasonable, until you remember it makes no sense with closures :3
- showell30 15y agoYou are correct that variable scoping goes all the way to the top, but nobody is forcing you to create top-level variables with overloaded names like "log". Seriously, if you have a file that uses log files and logarithms, just take some care to distinguish the concepts. Use "log_file" for log files; use "logarithm" or "Math.log" for inverse exponentation.
- xnxn 15y ago> Use "log_file" for log files; use "logarithm" or "Math.log" for inverse exponentation. Just clarifying; are you actually suggesting this, or was that sarcasm?
- showell30 15y agoIt's a serious suggestion. Instead of introducing four top-level variables (log, sin, cos, and tan), just introduce one (Math) that wouldn't possibly conflict with a local.
- the_mitsuhiko 15y agoThe CoffeeScript sourcecode itself does not advocate it. It uses functions named like 'last', 'starts', 'ends', 'extend' etc.
- showell30 15y agoYou are correct about "last", "start's, etc., but there are also good examples in the CS sourcecode of easily avoiding naming conflicts with a sensible naming convention: browser.coffee:CoffeeScript = require './coffee-script' coffee-script.coffee:{Lexer,RESERVED} = require './lexer' coffee-script.coffee: {Module} = require 'module' command.coffee:{EventEmitter} = require 'events' grammar.coffee:{Parser} = require 'jison' lexer.coffee:{Rewriter, INVERSES} = require './rewriter' nodes.coffee:{Scope} = require './scope' nodes.coffee:{RESERVED} = require './lexer' repl.coffee:{Script} = require 'vm'
- nas 15y agoYes, and it's very uncommon that you need to do that. I learned Scheme years ago before I learned Python (in 1993, I think). At first I was dismayed by Python's lack of non-local assignment. However, in years of Python programming I can count the times I needed the ability on one hand. I haven't yet found a need for 'nonlocal'. Obviously it depends on programming style. In Coffeescript you don't have something akin to 'self' and so assigning to a non-local happens a lot.
- masklinn 15y ago> However, in years of Python programming I can count the times I needed the ability on one hand. I haven't yet found a need for 'nonlocal'. Might have to do with Python itself as well: because it's function-scoped and it tends to avoid higher-order function (in part due to the limitations of its anonymous functions), there are far less occasions write to lexical closures than in Scheme, Smalltalk or Ruby.