-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Method reference : Enhancement or new feature #2769
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
Comments
oh I see. Then I actually have some more reading to do on Typescript On Tue, Apr 14, 2015 at 10:30 PM, Daniel Rosenwasser <
|
Sorry @Kavignon, I hadn't finished that response and but accidentally posted (and deleted) it. I realized what you meant in the middle of responding though, which is decidedly different from what I had understood. |
Based on what you understood, would you say that the topic I've raised is good enough to be discussed as a possible feature for either 1,6 or 2.0? @DanielRosenwasser |
Before I could comment on something like that, we'd need a formal proposal. One thing I'll mentions is that we tend to be conservative on new syntax to avoid conflicting with future versions of ECMAScript. An issue worth raising is whether this has a use-case outside of instance methods. |
actually almost all examples given are static methods normal way to reference an instance method in java is given these methods
these method references
would be equivalent to this
they aren't all that different from storing a function in a variable, but it allows you to use statics/instances in that way. in normal javascript if you've stored a function in a field you could just have a method reference via
which leads me to believe the js has little need for this as a feature syntax... but since typescript doesn't generally allow you to just get the reference in the field like this, it could use this feature. in es5 all you have are method references, but in typescript, you can't reference them, afaik. |
+1 |
A related feature #3508 might eventually supersede this issue. (I came here searching for method references and dug down for the related ecmascript proposals) |
+1 |
Java just started to support lambdas in its language, with this new feature, they brought into play some syntax sugar coating to make code more concise. I thought it look pretty neat and wanted to know if that could be an enhancement of the language.
So, for those who might not know what method reference actually is, I'm going to give a definition for this and then show you an example of how it is used.
Method reference : shortcuts that you can use anywhere you would use a lambda.
Oracle defines 4 types of method references
Reference to a static method (ContainingClass::staticMethodName)
Reference to an instance method of a particular object (ContainingObject::instanceMethodName)
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor (ClassName::new)
As an example,
Here's the first version in Java (Lambda)
list.sort(comparing( (Apple a) -> a.getWeight() ));
Now here's the version using method reference
list.sort(comparing(Apple::getWeight));
It just makes code a bit shorter when using lambda. I'm well aware that Typescript has delegates and that it could be encapsulated (lambdas) within a delegate. I stumble upon that Java 8 feature and thought that a similar feature would be quite nice.
I'd like to start a discussion on this topic.
The text was updated successfully, but these errors were encountered: