51724

Type Inference with Deconstruct tuple assignment extension methods

Given some extension methods:

public static TO ConvertValue<TI, TO>(TI value) => (TO)Convert.ChangeType(value, typeof(TO)); public static void Deconstruct<TI, TO1, TO2>(this IEnumerable<TI> src, out TO1 p1, out TO2 p2) { var e = src.GetEnumerator(); p1 = e.MoveNext() ? ConvertValue<TI,TO1>(e.Current) : default(TO1); p2 = e.MoveNext() ? ConvertValue<TI,TO2>(e.Current) : default(TO2); }

Why is it that the C# compiler is unable to infer the types for Deconstruct here:

(double p1, int p2) = new int[] { 1, 2, 3, 4 };

But has no problem inferring the types here?

Ext.Deconstruct(new int[] { 1, 2, 3 }, out int p3, out double p4);

Answer1:

From deconstructions (C# 7.0) documentation :

None of the parameters can be type arguments.

The resolution is equivalent to typing rhs.Deconstruct(out var x1, out var x2, ...); with the appropriate number of parameters to deconstruct into. It is based on normal overload resolution. This implies that rhs cannot be dynamic and that none of the parameters of the Deconstruct method can be type arguments. A Deconstruct(out T x1, out T x2) method will not be found.

Recommend

  • Using Python 3 with Python 2
  • Why Java compiler restricts adding even the right type of element to the List when using wildcard wi
  • Polymorphic recursion - syntax and uses?
  • How to specify column types for CTE (Common Table Expressions) in PostgreSQL?
  • What is the expected performance of IEnumerable?
  • Using a nested type parameter as a toplevel type parameter
  • What is the difference of having a DOCTYPE — and BackCompat vs CSS1Compat mode in Chrome, Firefox, a
  • Inferring destination type from interface with AutoMapper
  • JavaScript recursion base case
  • CNN Keras: How many weights will be trained?
  • Need to track PC down times using PHP ping and display time down D:HH:MM
  • Avoid Collection has been modified error
  • Why does EF Core always return -1 with this stored procedure?
  • T-SQL generated from LINQ to SQL is missing a where clause
  • Pass code to a method as an argument
  • plot if col A has substring
  • com.jcraft.jsch.JSchException: Algorithm negotiation fail
  • Eclipse, completion of inner classes
  • sort decimal in php or mysql
  • How to change the margins of a correlation matrix plot
  • Initialization section of the package
  • Receive list of elements in their visual order
  • Programmatically access files in Document set in sharepoint using Javascript
  • Cannot get the UserManager class
  • Linq Merge lists
  • Unable to decode certificate at client new X509Certificate2()
  • Display images in Django
  • Set the selected item in dropdownlist in MVC3
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Launch Runnable Jar from Web Start
  • Resize panoramic image to fixed size
  • Importing jscolor library in angular 2
  • How to delete a row from a dynamic generate table using jquery?
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Authorize attributes not working in MVC 4
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • EntityFramework adding new object to nested object collection