-
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
Please support 'var' as function return type instead of return a dynamic #13656
Comments
You are asking for three things:
|
The new tuples feature (#347) planned for C# 7.0 aims to improve this. With it, you will be able to write: void Main()
{
var name = Foo().Name;
}
public (string Name, int Age) Foo()
{
return (Name: "", Age: 0);
} It doesn't work in your exact case, because single-value tuples are not supported. |
@svick I can't help but think of this: public let Foo() => (Name: "", Age: 0);
The only solution is better method names I guess. |
The tuple is cool. But still need to define the item type and it can't work with complex type such as return new{a=new{b=1}}. |
@dsaf You can do it the other way around: public (string Name, int Age) Foo() => ("", 0); |
@AndyQi Tuple can work with complex type such as |
Re additional type inference, see my comment here: #17 (comment) |
Maybe related to #5161. |
I don't like that to find what a method returns I will need to scan through all its lines. |
@omariom Needn't scan all the lines, move the mouse pointer to the var or just call the method, then vs will tell you what the var is. |
@AndyQi I don't really agree with this proposal exactly, in the far past I'd probably agree with you on exposing anonymous types but since the introduction of tuples and future records I don't think it's worth the troubles but I do think that there's more room for more type inference, especially when it comes to generics but that's another issue. |
Dead code is easy to remove, you can do it manually through the IDE using tools like ReSharper, Roslyn Analyzers, or automatically in a build step or something.
That's similar to records, the only thing is you have to define it before its usage, what if two methods returns |
I too feel that it's often an overkill. Unfortunately the current practice is one file per type. |
@AndyQi Serialization is really a question of whether the serializer you use can handle it, the data is there. |
Why would you need to add it yourself? the compiler does it for you.. I'm not sure I understand you. |
@eyalsk What I mean is that the compiler doesn't do it, because it can't. This code: var result = (count: 5, sum: 20);
new JavaScriptSerializer().Serialize(result); is compiled into: var result = new ValueTuple<int, int>(5, 20);
new JavaScriptSerializer().Serialize(result); As you can see, the compiler didn't put |
I think this discussion belongs to #6949. Tuples should not be confused with "dictionaries". |
@svick You're right, my bad, didn't think this through so thank you for the enlightenment. :) |
I'm in favor of inferred function return types. There are quite many cases where specifying them just seems redundant. Actions in mvc being one of the examples: public let Index()
{
return View();
} Would be quite nice instead of specifying Many small applications and small functions would benefit from this feature. As for the concerns @agocke has mentioned, in case of bigger projects and perhaps class libraries where return types are part of the documentation, it would be really simple to build a roslyn analyzer that requires developers to specify them explicitly on any of the public functions. |
@TKharaishvili I understand what you're saying but tbh this is actually bad imo, I'm all for type inference but this seems like taking C# too far, way too far. What's the difference between Initially, iirc p.s. I think that there are few proposals discussing your suggestion that were closed because this is too radical. :) |
We have been migrating language discussions and proposals to the csharplang repo. I'll go ahead and close the present issue. |
Anonymous class is convenient, especially for linq. But it's not convenient to return an anonymous class in function. So I have to define a returned type class (boring) or use dynamic (lose VS autocomplete and compile checking).
Why not support 'var' instead of 'dynamic'? VS is able to recognize what the var is by return value, thus the Foo function returns a strongly-typed value, much better than dynamic and needn't define a class.
void Main()
{
var name = Foo().Name;
}
public dynamic Foo()
{
return new { Name = "" };
}
public var Foo()
{
return new { Name = "" };
}
Actually, what troubles me is defining the one-time used type. I must create a file or look for somewhere to write the code. And when the function is useless any more, I have to find the define and remove it.
Exposing anonymous types is one solution. Since you guys don't like it, I have another idea.
@dsaf @svick @ufcpp @agocke @eyalsk How about defining a class while return, like this:
public User Foo()
{
return new class User { Name = "" };
}
The text was updated successfully, but these errors were encountered: