Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this compiler error when mixing C# ValueTuple and dynamic

When using ValueTuple and dynamic object, I received this weird CS8133 error. I am passing dynamic object as input and taking ValueTuple as output. Why are they affecting each other.

public static (string, string) foo(dynamic input)
{
    return ("", "");
}

public void foo_test()
{
    dynamic input = new { a = "", b = "" };
    (string v1, string v2) = foo(new { a = "", b = "" }); //compiles fine
    (string v3, string v4) = foo(input); //CS8133 Cannot deconstruct dynamic objects
    var result = foo(input);  //compiles fine
}

Edit: The error message is: CS8133 Cannot deconstruct dynamic objects

like image 363
Xiaoguo Ge Avatar asked Jul 29 '17 02:07

Xiaoguo Ge


People also ask

Can C and C++ be mixed?

The C++ language provides mechanisms for mixing code that is compiled by compatible C and C++ compilers in the same program. You can experience varying degrees of success as you port such code to different platforms and compilers.

How do you call a C++ function that is compiled with C++ compiler in C?

Just declare the C++ function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code: extern "C" void f(int);

Can I use C libraries in C++?

Yes - C++ can use C libraries. Except of course that you're using the C++ version of cstdio . To use the C one you need to #include <stdio. h> .


1 Answers

See the feature spec:

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. ...

The part that is important is the var. In normal overload resolution, we can infer the type from the Deconstruct methods that are discovered. But with dynamic method invocation, you cannot get compile-time type information, so the var types are necessarily left un-inferred (ie. that's an error).

More generally, this is why you cannot use out var on a dynamic invocation (what is the var type of the out var local?).

like image 141
Julien Couvreur Avatar answered Oct 05 '22 22:10

Julien Couvreur