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 declaring type inferring for type members with in-place initializiers. #1042

Closed
sgjsakura opened this issue Mar 5, 2015 · 9 comments

Comments

@sgjsakura
Copy link

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.

public class Sample
{
  private var data = 1; // type: Int32
  private static var static_data = 1; // type: Int32
  private const data = 1; // type: Int32

  public var Data => 1; // type: Int32
  public var Data3 {get;set;} = 1; // type: Int32
  public var Data2 => Data2; // Maybe OK, but sometimes will cause cycle-dependency problem?

  public var Update() => new Point(1,1); // type: Point
  public var Update2() => Update(); // Maybe OK, but sometimes will cause cycle-dependency problem?
}

I don't know if this is feasible for compilers.

@paulomorgado
Copy link

Why do all but const require var? Or why doesn't const require var?

@svick
Copy link
Contributor

svick commented Mar 5, 2015

@paulomorgado One problem with const var is that it's an oxymoron: is the value constant or can it vary?

@gafter
Copy link
Member

gafter commented Mar 6, 2015

@svick var means "variable". It doesn't mean that it can vary. The name comes from mathematics, where the same name might (or might not) bind to different values at different times, but nothing varies or mutates.

@gafter
Copy link
Member

gafter commented Mar 6, 2015

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 A.N?

@sgjsakura
Copy link
Author

@paulomorgado Add var after const is also acceptable but seems to be redundancy. In VB when you use Const to declare variables you do not need to use Dim any more. Anyway, this is not a serious problem and they both are OK.

@sgjsakura
Copy link
Author

@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;

@thomaslevesque
Copy link
Member

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.

@paulomorgado
Copy link

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 var was presented and now use it all the time, I would use this.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

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.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants