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

Why not define a standard helper function #24

Open
tmikov opened this issue Feb 7, 2024 · 14 comments
Open

Why not define a standard helper function #24

tmikov opened this issue Feb 7, 2024 · 14 comments

Comments

@tmikov
Copy link

tmikov commented Feb 7, 2024

This is a continuation of the discussion here: #23 (comment)

There are a couple of obvious solutions that don't require changing the syntax:

  • Error.throwHelper(x) with special logic to discard the topmost frame.
  • Error.throwHelper(e) with an Error parameter.

@ljharb objected:

This [special logic to discard the topmost frame] is not an option, both because stacks aren't in the spec and because making a specific function magic in this regard would be profoundly problematic.

My response to that is that if stacks aren't in the spec, then 90% of the motivation for this proposal disappears. In any case it would be a QoI issue. It is not any more magical than Error already is.

@ljharb
Copy link
Member

ljharb commented Feb 7, 2024

Error is indeed already magical. New forms of magic must be avoided.

Stacks exist, so the motivation for this proposal is unchanged - not being in the spec just means we can't use the spec to alter or constrain them.

@rbuckton
Copy link
Collaborator

rbuckton commented Feb 7, 2024

This is already covered in the explainer: https://github.com/tc39/proposal-throw-expressions?tab=readme-ov-file#other-notes

@tmikov
Copy link
Author

tmikov commented Feb 7, 2024

@rbuckton I don't think it was satisfactory covered. Specifically this seems incorrect:

The __throw helper will appear in err.stack in a host environment.

@rbuckton
Copy link
Collaborator

rbuckton commented Feb 7, 2024

A user-defined __throw helper would, barring presence and use of Error.captureStackTrace. The 2nd and 3rd bullet points are relevant, however.

If an engine optimizes Error.throw(err), it must also be concerned with const __throw = Error.throw; __throw(err), or const obj = { throw: Error.throw }; obj.throw(err).

@hax
Copy link
Member

hax commented Feb 8, 2024

A real function/method has problem, but as I suggested, we could consider meta method: do.throw(error).

@ljharb
Copy link
Member

ljharb commented Feb 8, 2024

That would indeed be a viable approach - but only because it's syntax, which is what this issue seems to be trying to avoid.

@hax
Copy link
Member

hax commented Feb 8, 2024

I think people generally don't mind whether it's a real method, and not really try to avoid syntax. It's all about ergonomics -- instead of forcing parens (throw err), method (whether it's real or meta method) has better ergonomics.

@theScottyJam
Copy link

How do you want your pseudo function call, JavaScript style do.throw(...) or lisp style (throw ...) :)

@ljharb
Copy link
Member

ljharb commented Feb 9, 2024

@theScottyJam the former would be fine, but would be syntax, as would the latter.

@Jack-Works
Copy link
Member

I don't understand why you need to "discard the topmost frame" at the first place. Error stacks are bound to where they are created, not where they are thrown (and this is true for Firefox, V8, and Safari).

img

@ljharb
Copy link
Member

ljharb commented Feb 9, 2024

@Jack-Works your exact example shows it pointing to the throw e line.

@Jack-Works
Copy link
Member

@Jack-Works your exact example shows it pointing to the throw e line.

but the error stack is not

@ljharb
Copy link
Member

ljharb commented Feb 9, 2024

You're correct, but that still results in user confusion.

@Jack-Works
Copy link
Member

You're correct, but that still results in user confusion.

if it is a built-in function, this won't happen.

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

6 participants