2

ValueTuple as a new feature in C# 7.0 is having public method Create which helps to create ValueTuples (from singleton to octuple or more) on the other hand we can also use new to achieve the same results. I noticed these behave differently. I am trying to research is below implementation wrong or this is something as per design:

Method CreateOctuple() is working as expected:

private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> CreateOctuple()
{
     return new ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>(1, 2, 3, 4, 5, 6, 7, new ValueTuple<int>(8)); ;
}

Now, I tried to achieve same output using Create() method, unfortunately, it is complaining about the return type:

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

What is wrong here? enter image description here

P.S. All packages are up-to-date and I am using Visual Studio 2017 -latest release. enter image description here

As suggested by Svick

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

This prompts same compiler exception: enter image description here

Gaurav Arora
  • 2,243
  • 6
  • 40
  • 57

3 Answers3

5

The issue is that ValueTuple.Create already takes care of calling ValueTuple.Create on the 8th element. So while the correct type for 8-tuple is ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>>, you're creating ValueTuple<int, int, int, int, int, int, int, ValueTuple<ValueTuple<int>>>. The fix is just to remove the second call to ValueTuple.Create:

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

Or you could just use the tuple syntax, but I'm assuming you have a reason to avoid it:

static (int, int, int, int, int, int, int, int) OctupleUsingCreate()
{
    return (1, 2, 3, 4, 5, 6, 7, 8);
}
svick
  • 236,525
  • 50
  • 385
  • 514
  • I've already tried that. But it was not working. I updated the question with snapshot and the code as sugegsted. – Gaurav Arora May 21 '17 at 14:00
  • @GauravAroraa That's just a ReSharper bug. The code actually compiles and works fine. – svick May 21 '17 at 14:07
  • 2
    @GauravAroraa I have just [reported it](https://youtrack.jetbrains.com/issue/RSRP-464845). – svick May 21 '17 at 14:17
  • Code is compiled - on another system, where Resharper is not installed. – Gaurav Arora May 21 '17 at 14:28
  • @GauravAroraa Even if you have ReSharper installed, you can compile it. The red squiglies will still be there, but the build will succeed without errors. – svick May 21 '17 at 14:58
  • its compiled but tests are failing. I know it looks weird but its happening. Thanks for raising ticket with Resharper team. – Gaurav Arora May 23 '17 at 08:38
0

You need to update this package: Microsoft.Net.Compilers to 2.0 (you need to show pre-release). It would fix the error, check this here

Community
  • 1
  • 1
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

Note: This is not a problem with the ValueTuple library or the C# compiler. No error on TryRoslyn with:

using System;
public class C 
{
    private static ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> OctupleUsingCreate()
    {
        return ValueTuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
    }
}
Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40