Runtime: Question: What is the purpose of public method ValueTuple.Equals(ValueTuple other) ?

Created on 19 May 2017  路  13Comments  路  Source: dotnet/runtime

Hi there,
While looking into ValueTuple.cs class, I observed that there is one public method:

public bool Equals(ValueTuple other)
{
  return true;
}

This method always return true```. Just wanted to check what is the purpose of this method and why it is simply returningtrue`` ?

area-System.Runtime question

All 13 comments

The ValueTuple type represents a zero-element tuple (a.k.a. nuple). It exists, even though there is no special syntax for it in C#. Just like all the other ValueTuple types, it supports value equality. And since it doesn't have any data, two zero-element ValueTuples are always considered equals.

This probably isn't very useful on its own, but I can imagine some generic code where this becomes useful.

@svick - Thanks for the heads up. Would you mind to share generic code where this becomes useful?

Would you mind to share generic code where this becomes useful?

It's not that great of an example, but consider that you have a method that can memoize one-parameter functions:

```c#
public static Func Memoize(Func func)


How do you memoize two-parameter functions? You use two-tuple:

```c#
public static Func<T1, T2, TResult> Memoize<T1, T2, TResult>(Func<T1, T2, TResult> func)
{
    var memoized = Memoize(((T1, T2) x) => func(x.Item1, x.Item2));
    return (x1, x2) => memoized((x1, x2));
}

The one-parameter Memoize probably uses a dictionary, and this takes advantage of the fact that ValueTuple<T1, T2> supports equality, so it can be used as a key in a dictionary.

But that also meas that to memoize zero-parameter functions, you can use zero-tuple and it will work:

c# public static Func<TResult> Memoize<TResult>(Func<TResult> func) { var memoized = Memoize((ValueTuple x) => func()); return () => memoized(new ValueTuple()); }

Like I said, this is not a great example, but I think that this can actually be useful.

@svick - These examples are good and provide some inside. I am looking some kind Deep - Dive ValueTuple so, need more examples. Thanks you so much for your time and for these examples.

@jcouv

Future C# language features will likely instantiate ValueTuple'0 and we felt the comparison should be there for consistency with ValueTuple types of arity 1 through 7.
In other words, I don't expect it to be instantiated or compared presently (by user code).

One more thing related to @svick's example: ValueTuple'0 can be useful in functional-style programming.
Lukas Lansky had an article on that (using ValueTuple'0 instead of void, to represent () from functional languages).
See https://programming.lansky.name/void-free-style/

@jcouv - good to hear that future C# language feature would begin new era. I liked void free styling. On the same node one more unresolved question for ToString(), it always return "()" and it makes sense for ValueTuple'0 but what about other ValueTuple types (arity 1 through 7).

```
public override string ToString()
{
return "()";
}

This is happening here: 

public static void FindOddEvenBySingleNumber1(int number)
{
(int,string) oddOrEvenValueTuple = (number,IsOddNumber(number) ? "Odd" : "Even");
Console.WriteLine($"Provided number:{oddOrEvenValueTuple.Item1} is {oddOrEvenValueTuple.Item2}.");

        //always return "()"
        Console.WriteLine($"ValueType.ToString() method:{oddOrEvenValueTuple.ToString()}");

        //Make it Tuple using Extension
        var toTuple = oddOrEvenValueTuple.ToTuple();
        //returns everything
        Console.WriteLine($"Tuple.ToString() method:{toTuple.ToString()}");

    }

private static bool IsOddNumber(int number) => number % 2 != 0;
```

why this inconsistency, why we are not considering others 1 to 7 ?

@garora, sorry I didn't understand the question.
ToString on the various ValueTuple types is extensively tested. (1, 2).Tostring() will print "(1, 2)", and so on.
Please provide a concise example for your question.

You can also review the tests and point to any line that is unexpected. Note that the ValueTuple tests are modeled after the Tuple types and tests, so there is actually little room for deviation.

@jcouv - cheers, most of tests are working now. There was one type ;(
Tests with Octuple is failing, here is the one example with exception of return type:

public static (int, int, int, int, int, int, int, ValueTuple<int>) CreateValueOctTuple()
{
   return ValueTuple.Create(1,2,3,4,5,6,7,8);
}

Unless you're reporting an issue with the implementation, I feel those questions are more appropriate for stackoverflow.
Try ValueTuple.Create(1,2,3,4,5,6,7, ValueTuple.Create(8)) or ValueTuple.Create(1,2,3,4,5,6,7, ValueTuple.Create(ValueTuple.Create(8))) depending on what you're trying to do.

For more details on how to nest ValueTuple for long tuples (8 elements or more), see http://mustoverride.com/tuples_valuetuple/

@jcouv - thanks for sharing the link, still need to do some regress research as I am still facing lot of issues. These could be due to design of ValueTuples (not sure on this part) and could be overwhelmed with implementation. I will work-it out at my end and then will post here (when or if find the solution). I raised a question here: ValueTuple: what is wrong with creation of octuple?

Was this page helpful?
0 / 5 - 0 ratings