ZeroPhase
ZeroPhase

Reputation: 647

How to declare and instantiate N-tuples?

I've checked through the posts on here and none that I've found seems to address my specific issue. At the time of posting this I had misread the MSDN category, on N-tuples, and couldn't see an obvious reason for my N-Tuple not working. I'm trying to make a tuple of N size for the first time, and am getting errors when I try to assign values. I think I just have the syntax off by a bit.

My Tuple:

public Tuple<float, float, float, float, float, float, float, 
     Tuple<float, float, float, float, float, float, float, Tuple<float>>> 
     CollisionTuple = new Tuple<float, float, float, float, float, float, 
     float, Tuple<float, float, float, float, float, float, float, Tuple<float>>>
     (0, 0, 0, 0, 0, 0, 0, Tuple<float, float, float, float, float, float, float>
     (0, 0, 0, 0, 0, 0, 0, Tuple<float>(0)));

So, which part of my syntax is off? Everything before assigning a value to the tuple doesn't return an error.

Update: I just overloaded the method and went with an array for that collision detect.

Upvotes: 0

Views: 839

Answers (2)

Tomas Aschan
Tomas Aschan

Reputation: 60564

@dlev's answer is correct. However, I'd recommend creating a new type for this, to make your code much more readable:

class CollisionTuple : Tuple<float, float, float, float, float, float, float, 
                         Tuple<float, float, float, float, float, float, float, 
                           Tuple<float>>>
{
    CollisionTuple(float f1, float f2, ..., float f15) 
        : Tuple<(7 type arguments...)>(f1, f2, ..., f7, 
              new Tuple<(7 type arguments again...)>(f8, ..., f14,
                  new Tuple<float>(f15))))
    {
    }
}

That way, you can insantiate it like this:

// where the class is used:
CollisionTuple collTup = new CollisionTuple(0, 0, (etc...), 0);

If you know that you're often going to initialize it with zeros, you can even create a default constructor that does that for you:

// inside the class, with the other constructor still defined:
CollisionTuple() : CollisionTuple(0, 0, (etc...), 0) { };

which makes usage even easier:

var collisionTuple = new CollisionTuple();

Voilá! All those messy generics and counting arguments are just gone (or at least dusted under the rug...) and you can focus on the important parts of your code =)

Upvotes: 1

dlev
dlev

Reputation: 48596

You're actually really close, you just forgot a couple of news, and one of the Tuple<float> type arguments. This works:

public Tuple<float, float, float, float, float, float, float, 
 Tuple<float, float, float, float, float, float, float, Tuple<float>>> 
 CollisionTuple = new Tuple<float, float, float, float, float, float, 
 float, Tuple<float, float, float, float, float, float, float, Tuple<float>>>
 (0, 0, 0, 0, 0, 0, 0, new Tuple<float, float, float, float, float, float, float, Tuple<float>>
 (0, 0, 0, 0, 0, 0, 0, new Tuple<float>(0)));

As I mentioned in my comment, though, you should really try to simplify this, perhaps by creating a new type to encapsulate whatever the heck is going on here. The fact you need to debug the instantiation of this object is a bad sign.

Upvotes: 3

Related Questions