-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Do not require type specification for constructors when the type is known #35
Comments
How is this readable? I see people complaining that
is not readable (which I can't understand at all, as the full information is on the line -- it just avoids duplication). So the provided one wouldn't satisfy these guys, I guess. But okay, now to your second point: If I just omit the possibility of having overloads (and how that should be handled) then I don't see how this is more readable at all. First it looks like an anonymous class instance, which is a false friend (and how this ambiguity should be handled is also unclear). Second, even if some constructor is used, we need to know the available method signatures for knowing which constructor to call. Therefore no one can read the code just from that one line. So what are my doubts?
|
I don't like the fact that it clashes with anonymous objects. In the case you show with
If I write |
It seems that people want (this and other inference discussions) elements of Hindley-Milner, but not the total way it was implemented in F#. |
This is not intended to please the ones that don't like
I don't expect the compiler to read the developer's mind or beyond that. This would only be possible if the type of the variable/parameter/field has a publicly accessible constructor with that signature. This wouldn't be possible:
Overload resolution would be a bit harder but not impossible. As for readability, I don't think it's worth than other cases we already have. How would that work for
@dsaf, I don't know. It would be inferring the type of the parameter from the method definition. Not inferring the return type of anything. |
So you're saying that |
Yes, but in this case you explicitly specify the type, so it's obvious you want to call the one that takes a
But currently the compiler does bind to |
Oops! Missed that one. I don' think we can get around that one. Even if we could come up with an alternative to anonymous types ( Something like |
Wouldn't Anders Hejlsberg answer also apply constructors? |
VOTE UP. |
I think the problem can be solved by explicit Examples:
|
@AdamSpeight2008 Not really, as this is a new proposed syntax form (assuming we do what @neofuji suggests). It has no effect on overload resolution for existing code. However, the specification and implementation would likely be nearly as complex as the spec for the type of a lambda (where we have to essentially try every possibility to see what works). |
I think it is also useful for setting values to properties. Examples:
|
And return statements...
|
Isn't the type inference being inferred from the wrong direction? Eg Inference based on target type? |
It is expected behavior on this issue. |
That's axiom I was pointing out and mentioned by Anders.
|
This is not possible either, T F<T>() { ... }
var res = F(); Same would be true for |
@AdamSpeight2008 Now, lambdas are inferred from target type.
|
Do we consider the argument list when we are inferring the type? void F(Foo foo) {}
void F(Bar bar) {}
class Foo { }
class Bar { public Bar(int a) {} }
F( new () ); // OK?
class Foo { public Foo(double a) {} }
class Bar { public Bar(int a) {} }
F( new (1.0) ); // OK? Do we consider the object initializer when we are inferring the type? class Foo { public int P { get; set; } }
class Bar { }
F( new () { P = 1 } ); // OK?
class Foo { public double P { get; set; } }
class Bar { public int P { get; set; } }
F( new () { P = 1.0 } ); // OK?
class Foo { public double P { set {} } }
class Bar { public double P { get; } }
F( new () { P = 1.0 } ); // OK? Do we consider the collection initializer when we are inferring the type? void F(Foo foo) {}
void F(List<int> list) {}
F( new () { 1, 2, 3 } ); // OK? What about type parameters? void F<T>(List<T> list) {}
F( new () { 1, 2, 3 } ); // OK? In the above example, is there any mechanism to infer |
This is attractive as a counterpart to deconstruction, and we are considering it for a future version of C#. |
I think it is useful for array initialization. public int[] indices = new[256]; |
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. This corresponds to dotnet/csharplang#100 |
Do not require type specification for constructors when the type is known.
Imagine this case:
becomes:
Obviously, constructor arguments and object initialization would be allowed.
This might be a simple optimization given the above example, but there are longer type specifications out there.
But when applied to this:
it would become this:
which is a lot more readable. In cases like this, the type is not really important, its properties are.
The text was updated successfully, but these errors were encountered: