Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yes, but not quite. Consider the following example:

  static void Main()
  {
    Bar( 12, 15 );
  }

  static void Bar( object o1, object o2 )
  {
    Foo( o1, o2 ); // error
  }

  static void Foo( int x, double y )
  {
    Console.WriteLine( "int double" );
  }

  static void Foo( double x, int y )
  {
    Console.WriteLine( "double int" );
  }

  static void Foo( double x, double y )
  {
    Console.WriteLine( "double double" );
  }

  static void Foo( int x, int y )
  {
    Console.WriteLine( "int int" );
  }
This does not compile as the compiler does not know which of the four versions it should call. As jules said overloading is resolved at compile time by looking at the compile time types. Multimethods are dispatched at run time, by looking at the run time values. In this case that would be the "int int" version.


Thanks for the explanation.

That's cool and all, but why wouldn't I just define an interface with method foo(), then override foo on my classes? This feels like class-based OO turned inside out.


"At run time" is the key. Consider Java.

If Dog and Cat are subclasses of Animal (or implement an Animal interface), for example, I might want to use a "breed" method to attempt to breed a hybrid animal with the loyalty of a cat and the stealth of a dog.

  public Animal breed(Animal a, Animal b)
  {
  // return generic Animal
  }

  public Animal breed(Dog a, Cat b)
  {
  //return Cog
  }
But at compile time, if all I know is that I have two animals to breed, Java will ALWAYS pick the breed() method with the Animal arguments, even if the run-time types will be Dog and Cat.

To solve this problem in Java you have to delve into the hideous Java reflection object and litter your code with junk that you'd wish the language would figure out for you.

When you've used a language that is aware of types at run time and supports multi-method dispatch, anything else seems like it's half-baked OO.


And referring back to the original question on C#, you can do this using the "dynamic" keyword -

http://achoiusa.wordpress.com/2009/08/27/exploring-c-4-0-mul...


I'm not really up on this stuff, but that strikes me as an implementation detail. Does it really need a new name?




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: