POV-Ray : Newsgroups : povray.off-topic : C# 4.0 Default parameters : Re: C# 4.0 Default parameters Server Time
6 Sep 2024 13:18:59 EDT (-0400)
  Re: C# 4.0 Default parameters  
From: Darren New
Date: 4 Feb 2009 20:57:53
Message: <498a47a1@news.povray.org>
Mike Raiford wrote:
> No, I don't think you can. The closest you could get would be something 
> like a multicast delegate. But, then you really cannot get a return value.

I think you'd have to do something like this. First, you need an interface 
that defines all the possible functions you can call this way. That would be 
the equivalent of making all the objects subclasses of the same class in C++.

interface Istuff {
   int xyz(int x);
   int pdq(int x);
}

// Then a class that inherits from Istuff
class Mine : Istuff {
   int xyz(int x) { return x + 1; }
   int pdq(int x) { return x - 1; }
}

// Declare the type of a delegate that takes the object and integer and runs 
something.
delegate int do_it(Istuff on_what, int with_what);

// Declare two delegates, initializing them. One calls xyz, one calls pdq.
do_it use_xyz = ((Istuff on_what, int with_what) => on_what.xyz(with_what));
do_it use_pdq = ((Istuff on_what, int with_what) => on_what.pdq(with_what));

// Pick which one you want
void go_run_a_function(
   inputted_bool ? use_xyz : use_pdq,
   Istuff some_object, int parameter);


So, you can do it, basically by changing
   blah(object, parameter)
into
   object.blah(parameter)
with a lambda. But it's messy. Probably about as messy as going the other 
way in C++.  There's probably some fancy thing with generics you could do too.


-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.