L
Lee Crabtree
I'm almost positive that this isn't possible in C#, unless there's something
REALLY esoteric I haven't come across, but I was curious why something akin
to multimethods in Lisp isn't possible in C#. Specializing function arguments
by type is useful, but sometimes it would be really nice to be able to write
functions specialized on the values of arguments. By way of example, instead
of something like this:
public class Thing
{
public void DoSomething(int var)
{
switch(var)
{
case 0:
//do work for the value of 0
break;
case 1:
//do work for the value of 1
break;
default:
break;
}
}
}
That's an extremely simple and contrived example, but the point still holds.
If I've got a lot of those kinds of cases, this kind of function gets unwieldy
fast. Yes, I could write separate functions for the work to be done for
each value, but I'd still need some kind of steering function to pick the
right one. Every time I need to add support for a new value, I've got to
modify this function. In Lisp, I would probably do something akin to this:
(defmethod DoSomething ((var (eql 0)))
;do work for value 0
)
(defmethod DoSomething ((var (eql 1)))
;do work for value 1
)
(defmethod DoSomething ((var integer))
;just return
)
If I ever need to add support for a new value, I don't have to change any
existing code, I just have to write a new function specialized on the value
I want. That obviously could get really hairy for complex types, and there's
probably a whole host of reasons why this doesn't work in C#, but since I
don't know, I thought I'd ask.
Lee Crabtree
REALLY esoteric I haven't come across, but I was curious why something akin
to multimethods in Lisp isn't possible in C#. Specializing function arguments
by type is useful, but sometimes it would be really nice to be able to write
functions specialized on the values of arguments. By way of example, instead
of something like this:
public class Thing
{
public void DoSomething(int var)
{
switch(var)
{
case 0:
//do work for the value of 0
break;
case 1:
//do work for the value of 1
break;
default:
break;
}
}
}
That's an extremely simple and contrived example, but the point still holds.
If I've got a lot of those kinds of cases, this kind of function gets unwieldy
fast. Yes, I could write separate functions for the work to be done for
each value, but I'd still need some kind of steering function to pick the
right one. Every time I need to add support for a new value, I've got to
modify this function. In Lisp, I would probably do something akin to this:
(defmethod DoSomething ((var (eql 0)))
;do work for value 0
)
(defmethod DoSomething ((var (eql 1)))
;do work for value 1
)
(defmethod DoSomething ((var integer))
;just return
)
If I ever need to add support for a new value, I don't have to change any
existing code, I just have to write a new function specialized on the value
I want. That obviously could get really hairy for complex types, and there's
probably a whole host of reasons why this doesn't work in C#, but since I
don't know, I thought I'd ask.
Lee Crabtree