TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Reply
 
Thread Tools Display Modes
Old 17-10-2007, 08:41 AM   #1
William Stacey [C# MVP]
Guest
 
Posts: n/a
Named anonymous types??

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]




  Reply With Quote
Old 17-10-2007, 08:42 AM   #2
Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
Re: Named anonymous types??

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.stacey@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]
>
>
>
>


  Reply With Quote
Old 17-10-2007, 09:47 AM   #3
William Stacey [C# MVP]
Guest
 
Posts: n/a
Re: Named anonymous types??

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.

--
William 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.stacey@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]
| >
| >
| >
| >
|


  Reply With Quote
Old 17-10-2007, 01:50 PM   #4
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Named anonymous types??

William Stacey [C# MVP] <william.stacey@gmail.com> wrote:
> 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.


I'd rather make it easy to declare the same kinds of types in code.
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 - <skeet@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
  Reply With Quote
Reply

Thread Tools
Display Modes




New To Site? Need Help?

All times are GMT +5.5. The time now is 03:01 PM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional