Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create aliases for built in functions #363

Closed
FezVrasta opened this issue Jan 5, 2017 · 4 comments · Fixed by #410
Closed

Create aliases for built in functions #363

FezVrasta opened this issue Jan 5, 2017 · 4 comments · Fixed by #410

Comments

@FezVrasta
Copy link

FezVrasta commented Jan 5, 2017

I mean, if I use multiple times Math.floor, it would make sense to assign it to a variable and use the variable.

function(x, y,z) {
  return { Math.floor(x), Math.floor(y), Math.floor(z)  };
}

To

function(x, y, z) {
  let a = Math.floor;
  return { a(x), a(y), a(z) };
}
@FezVrasta FezVrasta changed the title Create aliases for built in features Create aliases for built in functions Jan 5, 2017
@kangax
Copy link
Member

kangax commented Jan 5, 2017

We can do that if we know that a function call is idempotent (i.e. has no side effects). In this case, Math.floor is a getter but if this was doSomethingDestructive(x) + doSomethingDestructive(y) then we go from a side effect that happens twice to a side effect that happens once, which changes behavior.

We can maintain our own list of pure functions and/or allow for specifying a whitelist via options.

@FezVrasta
Copy link
Author

Why you say so? You would alias the function before it's executed.

so:

let a = doSomethingDestructive;
a(x) + a(y)

The result would be the same.

@kangax
Copy link
Member

kangax commented Jan 9, 2017

Oh, you're right. I was thinking of getters (when foo.bar evaluation has a side effect). This should be safer to implement then, as long as safeGetters option is enabled.

@vigneshshanmugam
Copy link
Member

Gonna try it out. @kangax @boopathi

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

Successfully merging a pull request may close this issue.

3 participants