|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Named anonymous types??for doing projections (especially in Linq), but not being able to pass or return them is a bummer. Why couldn't they add something like an "AS" modifier like: var v = new {Name="", Age=0} as MyType; var v2 = Change(v); void MyType Change(MyType mt) { return new {mt.Name + "2", mt.Age += 1} as MyType; } In this case, MyType can only be "declared" the same way over the whole assembly. So you can use "as MyType" in multiple projections with compile-time checking. However, the compiler flags an error if MyType is not declared the same way each time, so the first declaration "sets" the type by name. In this case, it is not really anonymous anymore, however it adds ability to strongly type projections and ref them from across functions and assemblies. -- William Stacey [C# MVP] This makes sense, to a degree, but I think it would be easier to just
have a refactoring option in the IDE which would take an anonymous type and create a known type, and then replace the anonymous type in the code with the new known type. -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "William Stacey [C# MVP]" <william.sta***@gmail.com> wrote in message news:Oew180FEIHA.4684@TK2MSFTNGP06.phx.gbl... > Maybe an Oxymoron, but this would be very useful. Anonomous types are > great > for doing projections (especially in Linq), but not being able to pass or > return them is a bummer. Why couldn't they add something like an "AS" > modifier like: > > var v = new {Name="", Age=0} as MyType; > var v2 = Change(v); > > void MyType Change(MyType mt) > { > return new {mt.Name + "2", mt.Age += 1} as MyType; > } > > In this case, MyType can only be "declared" the same way over the whole > assembly. So you can use "as MyType" in multiple projections with > compile-time checking. However, the compiler flags an error if MyType is > not > declared the same way each time, so the first declaration "sets" the type > by > name. In this case, it is not really anonymous anymore, however it adds > ability to strongly type projections and ref them from across functions > and > assemblies. > > -- > William Stacey [C# MVP] > > > > Interesting. That might work, however I think there are few ways that could
break down: 1) It becomes an IDE feature instead of a language feature, so people not using VS are out of luck (i.e. notepad, vi, etc). 2) How would you do things like List<T> in other parts of your code before the replacement?: List<new{Name="", Age=1}> . That would get difficult. With the other way, you could do List<MyType> as normal. 3) You may have two anymous types with same "signature", but different uses. How would the IDE disambiguate them? For example, you may have different extentions methods on the two types. Using the "as" method, it would be explicit. Probably other issues. But heck, if they could work it out, I might like it over having no feature. -- Show quoteWilliam Stacey [C# MVP] "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in message news:83D51B75-A60E-4C97-AF64-AEC037AE06E2@microsoft.com... | This makes sense, to a degree, but I think it would be easier to just | have a refactoring option in the IDE which would take an anonymous type and | create a known type, and then replace the anonymous type in the code with | the new known type. | | | -- | - Nicholas Paldino [.NET/C# MVP] | - mvp@spam.guard.caspershouse.com | | "William Stacey [C# MVP]" <william.sta***@gmail.com> wrote in message | news:Oew180FEIHA.4684@TK2MSFTNGP06.phx.gbl... | > Maybe an Oxymoron, but this would be very useful. Anonomous types are | > great | > for doing projections (especially in Linq), but not being able to pass or | > return them is a bummer. Why couldn't they add something like an "AS" | > modifier like: | > | > var v = new {Name="", Age=0} as MyType; | > var v2 = Change(v); | > | > void MyType Change(MyType mt) | > { | > return new {mt.Name + "2", mt.Age += 1} as MyType; | > } | > | > In this case, MyType can only be "declared" the same way over the whole | > assembly. So you can use "as MyType" in multiple projections with | > compile-time checking. However, the compiler flags an error if MyType is | > not | > declared the same way each time, so the first declaration "sets" the type | > by | > name. In this case, it is not really anonymous anymore, however it adds | > ability to strongly type projections and ref them from across functions | > and | > assemblies. | > | > -- | > William Stacey [C# MVP] | > | > | > | > | William Stacey [C# MVP] <william.sta***@gmail.com> wrote:
Show quote > Maybe an Oxymoron, but this would be very useful. Anonomous types are great I'd rather make it easy to declare the same kinds of types in code. > for doing projections (especially in Linq), but not being able to pass or > return them is a bummer. Why couldn't they add something like an "AS" > modifier like: > > var v = new {Name="", Age=0} as MyType; > var v2 = Change(v); > > void MyType Change(MyType mt) > { > return new {mt.Name + "2", mt.Age += 1} as MyType; > } > > In this case, MyType can only be "declared" the same way over the whole > assembly. So you can use "as MyType" in multiple projections with > compile-time checking. However, the compiler flags an error if MyType is not > declared the same way each time, so the first declaration "sets" the type by > name. In this case, it is not really anonymous anymore, however it adds > ability to strongly type projections and ref them from across functions and > assemblies. Automatically implemented properties go *some* of the way to this, but they don't give the read-only nature that anonymous types have. Personally, if I'm going to need a name for the type, I'd rather have it described explicitly rather than somewhere in the middle of a method. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too |
|||||||||||||||||||||||