-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 declaring type inferring for type members with in-place initializiers. #1042
Comments
Why do all but |
@paulomorgado One problem with |
@svick |
It is unlikely we would do this, for two reasons. First, we believe that types provide valuable documentation at the class level. Second, it introduces the possibility of cycles in the analysis that the compiler must do to determine types: class A
{
public var N = B.M + 1;
}
class B
{
public var M = A.N - 1;
} Now, what is the type of |
@paulomorgado Add |
@gafter Yes I agree that cycle dependency is a serious problem. This may occurs on static members which crosses different classes (Initializer of an instance field has much more limitations than static fileds). Maybe the auto infering can be applied with some limitations, actually the most useful scene is just initialize an field with an default value like: private var level = 0; |
I would like this feature, but only for private members. For public members, I think it's better to be explicit; otherwise you can easily change the type of a member without realizing it, breaking compatibility with client code. |
All discussions around this, inevitable, come to that, @thomaslevesque. Although theoretically nothing prevents it to be public, the amount of work required by the compiler and the dangers involved. Being one of those that said "I'll never use that" when |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
C# 3.0 intruduced the keyword "var" for type inferring for local variables. Can it be extended into type members with in-place initializiers?
e.g.
I don't know if this is feasible for compilers.
The text was updated successfully, but these errors were encountered: