3 ms·
Cor does not has a keyword to declare variables, instead, Cor declares it for you the first time it is used. If a variable with equal name is declared in outer
by dchest 11y ago
Cor does not has a keyword to declare variables, instead, Cor declares it for you the first time it is used. If a variable with equal name is declared in outer scope the compiler will assume you are using the declared outside. Unless you write the variable as a simple statement. This technique is called variable announcing.
Sadly, it's the same mistake that CoffeeScript made: you cannot just introduce a variable, to be completely sure you didn't break anything, you'll have to search for all uses of it in outer scope above and inner scopes below.
https://donatstudios.com/CoffeeScript-Madness https://donatstudios.com/CoffeeScript-Madness
- yosbelms 11y ago"Variable announcing" solves that issue, please see the example in the following link: http://yosbelms.github.io/cor/docs/playground/index.html#link:---%0Aexample%20of%20announcing%20variable%0A---%0A%0AsomeVar%20%3D%200%0A%0A%0A%2F%2F%20see%20how%20to%20use%20outer%0A%0Afunc%20usingOuter()%20%7B%0A%20%20%20%20someVar%20%3D%200%0A%7D%0A%0A%0A%2F%2F%20see%20the%20compiled%20code%20in%20the%20right%2C%0A%2F%2F%20notice%20the%20declaration%20inside%20the%20scope%0A%0Afunc%20avoidingOuter()%20%7B%0A%20%20%20%20someVar%0A%20%20%20%20someVar%20%3D%200%0A%7D http://yosbelms.github.io/cor/docs/playground/index.html#lin...
- dchest 11y agoI understand that you can announce variable, but to be save, you'll have to announce it in every scope you want to declare it, so why not just use "var" or ":=" to declare and assign?
- yosbelms 11y agoLooking for simplicity. One of the goals of Cor is to have few keywords, to make it easy to learn, to get involved, and to work with. I had three possible solutions to solve the variable scoping problem. 1- `var` keyword. Like javascript. It assumes all variables are global if not declared with `var`. 2- `global` keyword. Like PHP. It assumes all variables are local unlike that is annotated with `global`. 3- Ruby-like, Coffeescript-like. A mixing of options number 1 and 3 was chosen. I will open an issue regarding this subject, considering your proposal for future versions.
- dchest 11y agoThank you. Actually, what I think the best way is to make it an error to use undeclared variable, unlike in JavaScript. If it's a global, is should be declared in global scope. I really like this way of declaring variables, so please consider it: someVar := 123 //declared and assigned someVar = 456 // reassigned anotherVar = 777 // error: undeclared variable