Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

CS2 Discussion: Features: Introduce keyword “using” for controlled scoping #57

Closed
itsMapleLeaf opened this issue Dec 5, 2016 · 4 comments

Comments

@itsMapleLeaf
Copy link

MoonScript has a using keyword, which allows you to create a new scope only pulling in the variables from the parent scope that you need, along with being able to pass in nil to create an entirely new scope.

local in Lua works the same way as let does in JavaScript, so there's no Lua-specific magic going on here. using is a compile-time feature which checks for the variables used outside of the using block against the ones provided, or lack thereof, and declares them locally accordingly.

Here's how it'd work in JavaScript, assuming we don't limit the keyword to just function definitions.

value = 'some global variable'

# the way things are now, no way to create a variable called `value` within a new scope
do ->
  value = 'not local :('

# so we add a using keyword!
# `using null` shadows all variables in the parent scope
using null
  value = 'I am local!'

# however, we might need to access variables from the parent scope,
# so we specify them
foo = 3

using foo, bar
  # we can just use `foo` without worrying about overwriting anything else
  value = foo + 7

Compiles to:

var value, foo, bar;

value = 'some global variable';

(function() {
  value = 'not local :(';
})();

(function() {
  var value;
  value = 'I am local!';
})();

foo = 3
bar = 7
(function() {
  var value;
  value = foo + bar;
})();

However, because there might be some random using function out there in the wild, a viable alternative is limiting the feature to functions, like MoonScript does.

value = 'Some global variable'

foo = 3
bar = 7

# `num` is a function argument
# `foo` and `bar` are the used variables outside of scope
add = (num using foo, bar) ->
  value = num + foo + bar
  value

console.log add 5 #=> 15
@triskweline
Copy link

I'm afraid this might be impractical due to JS not distinguishing between functions/methods and variables: It's all variables. JS programmers commonly use variables in external scopes to require and alias functions from other modules, like this:

requiredFunction1 = require(...)
requiredFunction2 = require(...)
requiredFunction3 = GlobalName.function

add = (num) ->
  ...

If we want to use using we would need to add all required or alias functions to the method signature:

requiredFunction1 = require(...)
requiredFunction2 = require(...)
requiredFunction3 = GlobalName.function

add = (num using requiredFunction1, requiredFunction2, requiredFunction2) ->
  ...

substract = (num using requiredFunction1, requiredFunction3) ->
  ...

@edemaine
Copy link

edemaine commented Dec 6, 2016

What about the reverse? Imagine a keyword shadow that lists variables from higher scopes that you want to shadow (instead of which ones you want to keep) within the block:

x = 5
shadow x
  x = 10
console.log x  ## prints 5

Oops, I guess I just re-invented let blocks...

@GeoffreyBooth
Copy link
Collaborator

Closing due to lack of interest, and the consensus from other threads that we aren’t revisiting scoping at this time.

@coffeescriptbot coffeescriptbot changed the title Introduce keyword "using" for controlled scoping CS2 Discussion: Features: Introduce keyword “using” for controlled scoping Feb 19, 2018
@coffeescriptbot
Copy link
Collaborator

Migrated to jashkenas/coffeescript#4949

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants