Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Methods with Tuples and Field Name Rules in C# 7.0

I was reading this page, which is officially referenced in the release notes of Visual Studio 17 RC, it states the following:

For the purpose of Overloading Overriding Hiding, tuples of the same types and lengths as well as their underlying ValueTuple types are considered equivalent. All other differences are immaterial.

When overriding a member it is permitted to use tuple types with same or different field names than in the base member.

A situation where same field names are used for non-matching fields between base and derived member signatures, a warning is reported by the compiler

When giving that a shot:

public abstract class Foo
{
    public abstract void AbstractTuple((int a, string b) tuple);

    public virtual void OverrideTuple((int a, string b) tuple)
    {
        Console.WriteLine($"First= {tuple.a}  Second = {tuple.b}");
    }
}

public class Bar : Foo
{
    public override void AbstractTuple((int c, string d) tuple)
    {
        Console.WriteLine($"First= {tuple.c}  Second= {tuple.d}");
    }

    public override void OverrideTuple((int c, string d) tuple)
    {
        base.OverrideTuple(tuple);
    }
}

I get the following errors:

Error CS8139 'Bar.AbstractTuple((int c, string d))': cannot change tuple element names when overriding inherited member 'Foo.AbstractTuple((int a, string b))'

and

Error CS8139 'Bar.OverrideTuple((int c, string d))': cannot change tuple element names when overriding inherited member 'Foo.OverrideTuple((int a, string b))'

The Questions are:

  1. Is the official design Notes wrong ? Or is this a behavior that is yet to be implemented in the official C# 7.0 Release ?

  2. If this is the correct behavior, does it make sense that the tuple Field Names has to be the same in the overridden method ? Keeping in mind that two methods with same Tuples (int a, int b) and (int c, int d) are not considered overload candidates and generate an error!

  3. Do we have official C# 7.0 Features documentation somewhere ?

like image 777
Zein Makki Avatar asked Nov 27 '16 16:11

Zein Makki


1 Answers

Is the official design Notes wrong? Or is this a behavior that is yet to be implemented in the official C# 7.0 Release?

As far as I can tell, the documentation is out of date. That document hasn't significantly changed since April 2016, while the error that you got (ERR_CantChangeTupleNamesOnOverride in Roslyn source) was introduced in August 2016 (don't be confused by the different error code, that was changed later).

If this is the correct behavior, does it make sense that the tuple Field Names has to be the same in the overridden method? Keeping in might that two methods with same Tuples (int a, int b) and (int c, int d) are not considered overload candidates and generate an error!

Yes, that seems to be the new intended behavior.

Do we have official C# 7.0 Features documentation somewhere?

Considering that C# 7.0 is still under development, documentation is an issue. An article about tuples in the official .Net documentation is currently under review, though it does not mention these issues at all.

So, the outdated document you found is probably the best source for now. I have created an issue asking for the documentation to be updated.

like image 135
svick Avatar answered Sep 27 '22 19:09

svick