How to avoid accelerating over typeliness with large C# Tuples?

ala*_*ere 1 c# tuples class instance


Motivation

I ?occasionally?, despite their inadequacies, find C# Tuples useful.


Of these times, there have been reasons for dabbling in large Tuples over more initially obvious approaches such as creating a specific class or struct or a general, for example tree like class.

They include being able to define Tuples in more places/scopes. Usually in more local and/or temporary scopes such as inside functions right where needed and invisible elsewhere, or as return types, even in array definitions. Tuples are also in some respects less bulky than normal classes and those with (instantiated?) nested classes. Sometimes, although often a weakness, even the data structure like feel of a Tuples members being automatically named Item1 Item2 etc... can be preferable.


Issue

Or more an issue of elegance, see 'motivation' above.

See this pseudo C# code:

var t = new Tuple<
    Tuple<float, float>,
    Tuple<
        Tuple<box, box, UInt16>,
        Tuple<float, float, float, float>>>
    ((1.5, 1.5), (
        (new box(2, 2), new box(3, 2), 4),
        (1.5, 1.8, 1.6, 1.8)));
Run Code Online (Sandbox Code Playgroud)

But as shown in the actual code below, the type name repetition required increases super-linearly to the number of nodes/members.

var t = new Tuple<
    Tuple<float, float>,
    Tuple<
        Tuple<Box, Box, UInt16>,
        Tuple<float, float, float, float>>>
    (new Tuple<float, float>(1.5, 1.5),
    new Tuple<Tuple<box, box, UInt16>, Tuple<float, float, float, float>>(
        new Tuple<box, box, UInt16>(new box(2, 2), new box(3, 2), 4),
        new Tuple<float, float, float, float>(1.5, 1.8, 1.6, 1.8)));
Run Code Online (Sandbox Code Playgroud)
nodes: 14
that are leaves: 8
types stated in first example: 14

types stated in the C-Sharp code: 38
    If all leaves were classes, like `box`: 45
    In a 31 node, 16 leaf, binary tree: 113
Run Code Online (Sandbox Code Playgroud)


It'll get worse still with more size, so far being 3.6x, imagine this line of code:

byte byte byte byt count = 4;
Run Code Online (Sandbox Code Playgroud)

?Question?

Is there a way to avoid this repetition of the specification of types?

p.s*_*w.g 5

If you just want to avoid having to retype the type name so many times, you can use Tuple.Create:

var t = 
    Tuple.Create(
        Tuple.Create(1.5, 1.5),
        Tuple.Create(
            Tuple.Create(new box(2, 2), new box(3, 2), (ushort)4),
            Tuple.Create(1.5, 1.8, 1.6, 1.8)));
Run Code Online (Sandbox Code Playgroud)

Note that the this will use type inference where possible, so if you need to generate a Tuple that uses a type that cannot be directly inferred from the parameter (such as a base type or a ushort in the case of the 4 literal), you have to do some deliberate casting or explicitly specify the types in that call to Tuple.Create.

Still, there are times where you have to spell out the entire type name, for example, in type members or method signatures. In this case, you can use an often overlooked feature of C#, the type alias:

using MyTuple = Tuple<Tuple<float, float>, Tuple<Tuple<box, box, UInt16>, Tuple<float, float, float, float>>>;
...

public class MyClass 
{
    public MyTuple MyFunc(MyTuple foo, MyTuple bar) { ... }
}
Run Code Online (Sandbox Code Playgroud)

The signature of the method shown above would be absolutely horrendous without the alias. And of course, you're free to mix and match aliases, too, meaning that complex generic class names can be broken down into simpler and simpler forms until they become more manageable.

However, I would still generally recommend using properly designed classes where possible.