Named anonymous types??

  • Thread starter Thread starter William Stacey [C# MVP]
  • Start date Start date
W

William Stacey [C# MVP]

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



message | 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]
| - (e-mail address removed)
|
| | > 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 said:
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.
 
Back
Top