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

Suggestion to simplify lambda and delegate syntax #40364

Closed
jnm2 opened this issue Dec 13, 2019 · 5 comments · Fixed by #58875
Closed

Suggestion to simplify lambda and delegate syntax #40364

jnm2 opened this issue Dec 13, 2019 · 5 comments · Fixed by #58875
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings Need Design Review The end user experience design needs to be reviewed and approved.
Milestone

Comments

@jnm2
Copy link
Contributor

jnm2 commented Dec 13, 2019

The aim of this issue is simplification, while the aim of the alternative https://github.com/dotnet/roslyn/issues/40366 is performance.

denotes where the suggestion span would be and _ denotes the spans that would be faded as unnecessary.

class C
{
    bool IsEven(int x) => x % 2 == 0;

    void Example1()
    {
        // 💡 Replace lambda with method group
        //                          _____↓↓↓↓↓↓___
        _ = new[] { 1, 2, 3 }.Where(n => IsEven(n));

        // 🛠 Fix result:
        _ = new[] { 1, 2, 3 }.Where(IsEven);
    }

    void Example2()
    {
        var neverReassigned = new Func<int, bool>(IsEven);

        // 💡 Replace lambda with delegate
        //                          _____↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓___
        _ = new[] { 1, 2, 3 }.Where(n => neverReassigned(n));

        // 💡 Replace lambda with delegate
        //                          _____↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓__________
        _ = new[] { 1, 2, 3 }.Where(n => neverReassigned.Invoke(n));

        // 💡 Replace Invoke method with delegate
        //                          ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓_______
        _ = new[] { 1, 2, 3 }.Where(neverReassigned.Invoke);


        // 🛠 Fix result:
        _ = new[] { 1, 2, 3 }.Where(neverReassigned);
    }

    void Example3()
    {
        var reassigned = new Func<int, bool>(IsEven);

        // No diagnostic
        _ = new[] { 1, 2, 3 }.Where(n => reassigned.Invoke(n));

        // 💡 Replace Invoke method with delegate
        //                          ↓↓↓↓↓↓↓↓↓↓_______
        _ = new[] { 1, 2, 3 }.Where(reassigned.Invoke);

        reassigned = n => !IsEven(n);


        // 🛠 Fix result:
        _ = new[] { 1, 2, 3 }.Where(reassigned);
    }

    void Example4()
    {
        var differentTypeWithSameSignature = new Predicate<int>(IsEven);

        // 💡 Replace lambda with Invoke method
        //                          _____↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓___
        _ = new[] { 1, 2, 3 }.Where(n => differentTypeWithSameSignature(n));

        // 💡 Replace lambda with Invoke method
        //                          _____↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓___
        _ = new[] { 1, 2, 3 }.Where(n => differentTypeWithSameSignature.Invoke(n));


        // 🛠 Fix result:
        _ = new[] { 1, 2, 3 }.Where(differentTypeWithSameSignature.Invoke);
    }
}
@jnm2 jnm2 changed the title Suggestion to replace lambda with method group Suggestion to simplify lambda and delegate syntax Dec 13, 2019
@JohanLarsson
Copy link

Note that this refactoring introduces allocations. Probably good to have a analyzer & fix for the other direction also.

@jnm2
Copy link
Contributor Author

jnm2 commented Dec 13, 2019

Good point. Also, some of the above cases are neutral or remove allocations. (I ran across .Select(foo.Invoke) where .Select(foo) would have worked.)

@CyrusNajmabadi
Copy link
Member

Note: i did write this feature in the past (i think the code is still in roslyn). It just didn't ship because of hte allocation issue.

@CyrusNajmabadi
Copy link
Member

Good point. Also, some of the above cases are neutral or remove allocations. (I ran across .Select(foo.Invoke) where .Select(foo) would have worked.)

Too niche IMO.

@jnm2
Copy link
Contributor Author

jnm2 commented Dec 13, 2019

Avoiding allocations is starting to seem more interesting to me than simplification. Alt: https://github.com/dotnet/roslyn/issues/40366, expecting to close this issue given the history Cyrus cites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature Request IDE-CodeStyle Built-in analyzers, fixes, and refactorings Need Design Review The end user experience design needs to be reviewed and approved.
Projects
Status: Complete
Development

Successfully merging a pull request may close this issue.

4 participants