Sunday 15 February 2015

c# - Dynamic method dispatch based on value of variable -


Long switch statements are often frowned upon using solution polymorphism However, what I'm switching over is one Not a type of code? What I would like to do is replace the switch statement with something like this ... Public Zero Handelring (String S = "Hello") {...} Public Zero Handstring (String S = "Bye") {...} ... Handlists ("Hello"); // The first method is being called

This will replace the following ...

  string s = "hello"; Switch (s) {case "hello": ... break; The case "goodbye": ... break; Default; break; }  

Any ideas? In theory I think that you can completely remove the 'if / switch' statements and call only the methods which are automatically bound based on the value of the expression.

There are languages ​​that implement such words. Right from now I am familiar with the compiler generator tool called elegant.

In this type of language, a simple factual algorithm may look like:

  Facts (value: IT): int condition values ​​<0 {{" Value {return value} = 0}: return {0 return} fact (value = 1): difference {return1} facts (value: int): int {return value * fact ( Science - 1);}  

No comments:

Post a Comment