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

Proposal: Allow "this" as the constructor name #1335

Closed
MV10 opened this issue Feb 22, 2018 · 8 comments
Closed

Proposal: Allow "this" as the constructor name #1335

MV10 opened this issue Feb 22, 2018 · 8 comments

Comments

@MV10
Copy link

MV10 commented Feb 22, 2018

Some languages use the this keyword as the name of the constructor. It seems sensible to me so I thought I'd float the suggestion. I suppose it also implies ~this for finalizers.

public class Foo
{
    public this(string bar) { ... }
}

While building and maintaining DI-aware libraries, I often find myself cutting-and-pasting constructors and this change would save a (small) step having to rename the constructor to match the new class.

@HaloFour
Copy link
Contributor

A this member already means something different in C#, defining an indexer. I think it would be confusing to reuse it to also mean a constructor.

@MV10
Copy link
Author

MV10 commented Feb 22, 2018

Ah, great point. Closing.

@MV10 MV10 closed this as completed Feb 22, 2018
@jnm2
Copy link
Contributor

jnm2 commented Feb 22, 2018

I often find myself cutting-and-pasting constructors and this change would save a (small) step having to rename the constructor to match the new class.

💡 What about copying and pasting only the constructor parameter list, and then using a code action to generate fields from the parameter list?

@TruePluto
Copy link

may be using ctor instead of this

public class Foo
{
        public ctor(string bar){....}
        public ctor(string bar, string bar2) :this(bar){...}
        public ctor():base(){...}
}

@CyrusNajmabadi
Copy link
Member

Note: there is nothing actually syntactically wrong with allowing 'this' as the constructor name. yes, it shares a prefix with indexers. but they have very different code after the 'this' keyword. 'this' works well for me in both situations. One explains how to construct a new 'this', and one explains how you could index as instance of 'this'.

@MV10
Copy link
Author

MV10 commented Mar 2, 2018

@TruePluto I've had the same thought ... but they try to avoid adding new keywords. (That was explained to me once when I asked about the weird way that using has two wildly different meanings.)

@CyrusNajmabadi That's a good point. Indexers have a return-type, so without a type it could indicate the constructor.

@CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi That's a good point. Indexers have a return-type, so without a type it could indicate the constructor.

Which is exactly the same grammatical difference between constructors and methods today :) So it makes a bunch of sense.

@GeirGrusom
Copy link

new

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

6 participants