Is there a conceptual reason why calling a method with a dynamic parameter always returns dynamic?

46 views Asked by At

I can imagine this would be a problem with multiple overloads, but (besides Linq) a big part of the code probably has only one overload.

When there is only one overload it can save extra casting boilerplate, avoiding errors like Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type. in situations.

Of course you could argue that when you add an overload at a later stage the code would i.e. start triggering the above compiler error again. But then again - adding overloads can also break typed behavior (with i.e. multiple interfaces).

Is this just not a priority (for now) or am I missing some conceptual problem? I'm asking this to learn more about language design.

static int MapLolCats(int amountOfLolcats, Func<dynamic,int> mappingFunction)
{
    return mappingFunction(amountOfLolcats);
}

// This works as expected
var amountOfLegs = MapLolCats(5, x => x * 4);

// This creates a compilation error (despite you could deduct what is meant, because there is only one overload)
// Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
dynamic jsonValue = 5;
var amountOfLegs = MapLolCats(jsonValue, x => x * 4);
0

There are 0 answers