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

Compiler shouldn't allow extension method called "Clone" on records. #48287

Closed
Youssef1313 opened this issue Oct 3, 2020 · 6 comments
Closed

Comments

@Youssef1313
Copy link
Member

Currently, the following code compiles without errors:

public record S;

public static class Extensions
{
    public static S Clone(this S s)
    {
        _ = s;
        return new S();
    }
}

SharpLab.

@CyrusNajmabadi
Copy link
Member

Closing out as i do not see any language rules we've specified that would disallow this. If you would like to propose a language rule here, please open a discussion over at dotnet/csharplang. Thanks :)

@Youssef1313
Copy link
Member Author

@CyrusNajmabadi I just came across #40726 and found:

  • Clone extension method allowed? (no)

image

Did that meant records shouldn't indeed have Clone extension methods? Why it has a check mark while it doesn't seem to be done yet?

cc: @jcouv

@jcouv
Copy link
Member

jcouv commented Oct 15, 2020

This was checked because the question was resolved ("no"). That test plan tracks the C# 9 work for records.

@Youssef1313
Copy link
Member Author

Youssef1313 commented Oct 15, 2020

@jcouv Clone extension methods on records are currently allowed. Is there any work/tracking issue to disallow it?

@CyrusNajmabadi
Copy link
Member

What we we decided is that when looking up the Clone method we wouldn't bind to extension methods. It's not that you can't write the extension method. It's just that the compiler won't use it.

@Youssef1313
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants