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

Should we allow function.sent in arrow functions? #9

Open
hax opened this issue May 7, 2020 · 4 comments
Open

Should we allow function.sent in arrow functions? #9

hax opened this issue May 7, 2020 · 4 comments

Comments

@hax
Copy link
Member

hax commented May 7, 2020

We can refer other meta properties in arrow functions, for example:

function X() {
  return () => new.target
}
new X()() // X

Shall we also allow it for function.sent?

Possible usage: short alias for function.sent

function *numberToken() {
  const v = () => function.sent

  let sign = 1, integer = 0, fraction = 0
  if (v() === '-') {
    sign = -1
    yield
  } else if (v() === '+') {
    yield
  }
  while (v() >= '0' && v() <= '9') {
    integer *= 10
    integer += v().charCodeAt(0) - '0'.charCodeAt(0)
    yield
  }
  if (v() === '.') {
    yield
    let x = 1
    while (v() >= '0' && v() <= '9') {
      x *= 10
      fraction += (v().charCodeAt(0) - '0'.charCodeAt(0)) / x
      yield
    }
  }
  if (v() === undefined) return sign * (integer + fraction)
  throw new Error()
}

Problem: arrow functions are also "function" so programmers may expect they have own function.sent? And there will be generator arrow functions and they should have their own function.sent... (This is just again a naming issue like #1 (comment))

Alternative: make function.sent a function instead of a changing value so we can just write

const v = function.sent

(Another possibility is do not change anything, wait for refs proposal const ref v = ref function.sent)

@ljharb
Copy link
Member

ljharb commented May 7, 2020

In your first example, i have no idea why that's a function rather than just a variable (const v = function.sent).

Generator arrows (if they ever manifest) would need their own function.sent, but it'd be possible for them to bind it while normal arrows would not (this does complicate the naming issue).

I'm very confused why it would need to be a function in any case.

@hax
Copy link
Member Author

hax commented May 8, 2020

i have no idea why that's a function rather than just a variable (const v = function.sent).

Sorry I wrote a bad example, fixed now.

why it would need to be a function in any case

Currently we define function.sent as a value which will be auto updated after yield, so there is no simple way to alias it, u need manually re-assign after any possible yield.

PS. Make it a function also provide possibility of rich semantic, for example, it could have arg. See #3 (comment)

@ljharb
Copy link
Member

ljharb commented May 8, 2020

sure, you could think of it as a getter on function.

I think those "richer semantics" would be very confusing. Requiring that you save it in a variable if you need it across yields seems reasonable to me.

@hax
Copy link
Member Author

hax commented May 8, 2020

you could think of it as a getter on function.

Yeah, but it still not solve the "no simple way to alias it" problem. And if we think it as getter on function, it's no much difference to change it to method on function 😆

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

No branches or pull requests

2 participants