Infer "var" in "Func<var>" from a return value of lambda; Add a possibility to alias local anonymous types. #1129
Replies: 4 comments
-
These are language specification concerns as it is the language specification that forbids both |
Beta Was this translation helpful? Give feedback.
-
what happens if you declare |
Beta Was this translation helpful? Give feedback.
-
My proposal wasn't about public anonymous types (though I would like it too because if in C#7 I'm able to return valueTuple (which is a structure) from a function then I should be able to do the same with a class).
It relates not only to Func, but also to local functions.
My proposal is to allow to give a name\alias for anomymous types within a method:
Of course it would not make sense because outside of methods there are no anomymous classes. Func< var > would make a big sense if var should supposed to be an anonymous class. |
Beta Was this translation helpful? Give feedback.
-
in c#7 you can write local functions. but you cant return anonymous type. I suggest you to use tuple instead. your syntax
becomes
currently its not possible to use |
Beta Was this translation helpful? Give feedback.
-
I don't know should this proposal be a language feature or it mostly relates to a compiler.
I don't know why 'var' as a result type of 'Func' is disallowed because it can obviously be inferred from a lambda-expression.
Func<int, var> getSqrOfInt = x => x * x;
In vs2017 version 15.4.4 it returns a compile-time error "The contextual keyword 'var' may only appear within a local variable declaration or in script code";
And let's see that code that I want to use, it has a reusable part as Func that should do "select And Concatenate All KittenNames By Every CatMother":
but it doesn't compile because of
Func<IEnumerable<Cat>, IEnumerable<var>>
Instead I should use this code that duplicates a part of code that should do "select And Concatenate All KittenNames By Every CatMother" :
This was my first proposal and it somehow relates to #265 and #943 (except that mine is about Func-s and their proposals are about local functions).
Second proposal: what I want to do next is to make a part that should do "concatenate all CatMothers And Their Kittens" also reusable as Func:
but I can't because a first Func returns an anonymous type and I can't declare a second Func with an anonymous type parameter.
It can be fixed by introductions of anonymous types aliases in scopes of local functions.
Second proposal relates to this proposal for aliases #430
Beta Was this translation helpful? Give feedback.
All reactions