martin0 martin0

Niner since 2009


  • Inside C# 4.0: dynamic typing, optional parameters, covariance and ​contravaria​nce

    Regarding the complexity of overload resolution, it doesn't seem quite as bad as made out in the show (espcially compared to writing the compiler in the first place)

    Please note I am assuming you can't mix named and non named args in the same call - that would certainly complicate things.


    I agree the first thing is to consider the parameter names available to filter out invalid candidates.

    Second I would use the types to filter candidates further (considering valid implicit casts and base classes) This didn't seem to be mentioned.

    Then I would represent the candidate methods with a integer (with lots of bits) using 1 bit per arg.  Turn on these bits for each arg that is matched (by name) in the call. Turn on other bits that do not have an arg supplied in the call if they have a default value.

    Compare those integers with a mask representing the number of args in the candidates. Those that have all their args fulfilled remain candidates.

    If there is still more than one candiate overload, either throw an error, or have a policy to select the method.


    I'd be interested to get comments on this approach.

    Apologies if I've opened a can of worms. I've only given this 5 mins thought, but I have also written a complier.