Replies: 19 comments
-
As tuples are an autogenerated type with effectively random names and are not shared between assemblies it would be quite dangerous to expose them outside of the method in which they are used. A recompilation of a library would almost certainly lead to breaking every consumer of that library. |
Beta Was this translation helpful? Give feedback.
-
Why is that an issue? Are you concerned about the performance of copying a large tuple? Are you sure using a reference type (which means less copying, but at the cost of GC pressure) would be more efficient? |
Beta Was this translation helpful? Give feedback.
-
@svick There is. I was heavily use around 50 of anonymous objects. each of those contain some strings, large struct with 44 floats in the very complex linq to ordering, filtering, grouping and so on. Is was returning and passing in function I made a class for it but I just wish it could be anonymous |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@Thaina any value type can be boxed or can be passed by reference as the method argument. Personally, I often use |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@Opiumtm It's not. It's opposite I don't want just a sugar for declaration. I want to have anonymity. I also don't want to handle location of declaration. I just want to have reference type tuple. We don't need a name for float X and float Y. Just sometimes it should be valuetype and sometime it should be reference type It should be as easy as using tuple |
Beta Was this translation helpful? Give feedback.
-
If you have a type with 44 different float properties, it should definitely not be anonymous. |
Beta Was this translation helpful? Give feedback.
-
@MgSam I have struct (with proper name) with 32 float and 12 float each struct Status { /* 32 floats */ }
struct Parameter { /* 12 floats */ }
/* combined size 44 floats */
var obj = new { string id,string name,string data,Status status,Parameter parameter }
/* always copy 44 floats */
var tuple = (string id,string name,string data,Status status,Parameter parameter) Like this I want it anonymous because it just a package of something that I would use in grouping and sorting. I want it reference because it will go in and out of long chained linq. struct in Tuple means it will make ValueType copy |
Beta Was this translation helpful? Give feedback.
-
Also I forgot to mention but another problem of ps Just seeing this was also mentioned in dotnet/roslyn#20136 |
Beta Was this translation helpful? Give feedback.
-
Why you think that Tuple is valueType? I don't know why but earlier I too thought that Tuple is valueType. But I opened declaration of Tuple and see that it is declared as a class: |
Beta Was this translation helpful? Give feedback.
-
The C# language feature for tuples don't use |
Beta Was this translation helpful? Give feedback.
-
@HaloFour yeah, you are right, ValueTuple is a structure, Tuple is a class. But still it is too strange for me why earlier I always thought that when I create tuples they are structures, it is strange because as I understand ValueTuple is a new thing and Tuple is an old one so I had to use a Tuple class, not a ValueTuple. And, thus if anonymous type declaration will be added in next versions of C# then:
|
Beta Was this translation helpful? Give feedback.
-
I severely doubt it. There's no reason to introduce a second flavor of tuples. The concept of exposing anonymous types as described is flawed. If they are publicly a part of a signature then they can no longer be anonymous. That puts a lot of burden on the language specification to describe a deterministic way to define these types so that a simple recompilation doesn't break every downstream consumer. As such I can't imagine it ever being adopted. |
Beta Was this translation helpful? Give feedback.
-
These types (if they will be introduced sometime) can't be called anonymous. Anonymous types still will stay available only from inside a function. But types that will be defined on one line with a function name will be available not only from inside that function's scope but also from outer scopes and that type we can't call 'anonymous', we should call them 'reference tuples', and tuples of ValueTuple we should call 'value tuples'.
|
Beta Was this translation helpful? Give feedback.
-
Without a name they are effectively "anonymous" and that's where the problem lies. All types require a name. With current anonymous types that's not an issue since they can't be a part of a signature so it doesn't matter how the compiler generates the name or the content of the type. But as soon as you expect to use it within a signature that name becomes critically important as all consuming code references that type by it's fully qualified name. I'm of the opinion that in these cases that it is preferable to declare the actual container type and instantiate it directly. That is currently more verbose, but upcoming language features should help with that: Today: public class NumAndName
{
public int num;
public string name;
}
public NumAndName GetNumAndName()
{
return new NumAndName { num = 1, name = "User" };
} With #100 (target-typed new expressions): public class NumAndName
{
public int num;
public string name;
}
public NumAndName GetNumAndName()
{
return new { num = 1, name = "User" };
} With #39 (records): public class NumAndName(int num, string name);
public NumAndName GetNumAndName()
{
return new(num: 1, name: "User");
} |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I think your final example won't work, as records are immutable, it should be |
Beta Was this translation helpful? Give feedback.
-
Oops, you're right. Fixing. |
Beta Was this translation helpful? Give feedback.
-
I think the point is, I don't care if in CLR it would really have a name and location. It just that, an object like this should not need a care from us programmer. It should have the same name when it has the same public signature Maybe we should just change algorithm to generate that name by hashing all the member names and type. And changing that might not be breaking change because in all along all those anonymous types are not exposed This logic for name generation should be implemented and we could also have an anonymous valuetype too |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Most of the time I play with anonymous type is really satisfied. I could write a long chain of linq or async code without caring for modifying complex type
Until I need to refactor and make a function out of it then it lose the very precious anonymity
I wish we could declare anonymous type outside function code. Such as
Tuple seem cover this issue but the problem of tuple is it is valueType and the old library (such as dictionary) does not support return
ref struct
. We don't have a way to create reference type tupleSorry if this is duplicate. I could not find any issue like this by searching for Anonymous type
ps. Also I wish we should have a feature for mutable reference tuple. If we would create new syntax for this
Anonymous type declaration
. I think it should be mutableBeta Was this translation helpful? Give feedback.
All reactions