Skip to content

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

Closed
Kavignon opened this issue Apr 15, 2015 · 8 comments
Closed

Method reference : Enhancement or new feature #2769

Kavignon opened this issue Apr 15, 2015 · 8 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Kavignon
Copy link

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.

@Kavignon
Copy link
Author

oh I see. Then I actually have some more reading to do on Typescript
because I completely over seen that.

On Tue, Apr 14, 2015 at 10:30 PM, Daniel Rosenwasser <
notifications@github.com> wrote:

I'm well aware that Typescript has delegates

What do you mean by "delegates" exactly?

I think what you really want is to do is capture this properly when you
use a method.

For instance,

class Apple {
constructor(private weight: number) {
}

public getWeight() {
    return this.weight;
}

}

So currently, if you have:


Reply to this email directly or view it on GitHub
#2769 (comment)
.

@DanielRosenwasser
Copy link
Member

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.

@Kavignon
Copy link
Author

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

@DanielRosenwasser
Copy link
Member

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.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 15, 2015
@xenoterracide
Copy link

xenoterracide commented Sep 15, 2016

actually almost all examples given are static methods normal way to reference an instance method in java is this::methodName (unless your reference isn't this ). Seems like they would be very useful in chaining with promises

given these methods

    toHal( json: any ): HalJson {
        let hal = new HalJson( json );
        this.log.debug( "hal", hal );
        return hal;
    }

    static toJson( response: Response ): Promise<any> {
        return response.json();
    }

    static toLinks( hal: HalJson ): Links {
        return hal._links;
    }

these method references

        this.container.registerTransient( publicKey, () => {
            return this.client.fetch( '' )
                .then( App::toJson )
                .then( this::toHal )
                .then( App::toLinks )
                .then( links => links["public"]);
        });

would be equivalent to this

     this.container.registerTransient( publicKey, () => {
            return this.client.fetch( '' )
                .then( response => App.toJson( response ) )
                .then( json => this.toHal( json ) )
                .then( hal => App.toLinks( hal ) )
                .then( links => links["public"]);
        });

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

    this.halJson = function( json ) {
        let hal = new HalJson( json );
        this.log.debug( "hal", hal );
        return hal;
    }

   ...
   .then( this.halJson )

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.

@jan-swiecki
Copy link

+1

@karlthepagan
Copy link

A related feature #3508 might eventually supersede this issue.

(I came here searching for method references and dug down for the related ecmascript proposals)

@simonfxr
Copy link

simonfxr commented Jul 1, 2020

+1

@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants