-
Notifications
You must be signed in to change notification settings - Fork 6
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
Built-in extension functions #17
Comments
Thank you for your suggestion. These are excellent examples of extension methods. Although these can all be implemented in user-land, so there's no urgency to include them in the proposal, and as I understand the tc39 proposal process, these features could be considered for future proposals. However, I agree with your idea that we need more examples like these to demonstrate the potential capabilities of extensions. I'd like to add them to my slides and present them in the future tc39 plenary.
I am currently refining the proposal and hope to update and advance it at next year's meeting. My current work focus on the core semantics and syntax. For example, the syntax of the proposal will change. The original proposal followed the bind operator's Although I do not plan to include built-in extension methods in the current proposal, there are indeed some issues that need to be considered in advance. Since the proposal is shifting to a dot-based syntax, the initial ad-hoc definitions and binary forms (such as |
It's possible to do this: for code value:takeIf() // call Function.takeIf
{
const takeIf = () => ...
value:takeIf() // call the function above
} |
@Jack-Works We can't use But as I presented in stage 1 slides, it's really a footgun that |
I feel |
JS already used almost all symbols, so some symbols are overloaded. For example,
No, label must be a valid identifier. Anyway I think we could postpone this part, let's first focus on |
I'm a big fan of the idea of extension functdions and accessors. I even posited the idea for virtual accessors back on the old bind operator proposal, so I'm glad to see they made it into this one. Every time I use Kotlin for something, it reminds me to check up on how this proposal is doing! It looks like there's been a bit of a stall, but I'm hopeful it will move forward at some point.
One thing I think might help is if there are some built-ins to go along with the concept in the proposal. These could help demonstrate the usefulness and expressiveness of extensions. For example, Kotlin has a few that are designed for avoiding creating temporary variables. Taking a leaf from that book, here are a few that could be built-in for JavaScript.
apply
Calls a function with the context passed as the first argument, always returns the context. This would be your most basic extension function that you use when you don't want to create one for such a simple task.
let
Similar to
apply
, but returns the result of the provided function rather than the context. Pairs well with optional chaining to avoidif
checks for nullish objects:takeIf
Returns the context only if the predicate function returns
true
, else returnsundefined
.WeakSymbol()
Creates an accessor that lets you attach data weakly to an object.
This would be syntactic sugar for, roughly (according to the current proposal), the following:
However, it would also mirror the existing
Symbol()
constructor, withWeakSymbol.for(key)
andWeakSymbool.keyFor(::extension).
That's it for now. I'll update this if I think of any more, and please feel free to chip in ideas of your own.
The text was updated successfully, but these errors were encountered: