Skip to content

Method reference : Enhancement or new feature #2769

Closed
@Kavignon

Description

@Kavignon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions