I just read a chapter in a programming book about Dynamic types. Although they are quite neat I cant think of a single real world example where I would use them. Does anyone here actually use them and for what?
dynamic to evaluate logical expressions represented in an object graph, where using the
dynamic keyword allowed me to largely ignore the types of the operands.
For example, if my expression graph was an arithmetic expression with a multiplication operator (e.g.
12 * 1.3), I would use
dynamic to store the operands
dynamic again to catch the result from the multiplication.
dynamic I would be forced to deal with concrete numeric types (
float, etc...) and the resulting code bloat. My
dynamic solution requires very little code and is much easier to grasp and maintain.
My initial idea was to turn my logical expressions into snippets of Python or Ruby, then run these snippets through IronPython or IronRuby to evaluate the result. It struck me all I required from Python or Ruby was their dynamic typing, so I was very happy to see that
dynamic affords Python-like typing for C#.
I cant think of a single real world example where I would use them
It's a framework, it isn't always about you. In the case of
dynamic alot of the backing came from the need to support more dynamic languages in the CLR, such as F# or IronPython. The fact that it helps cut down on code in other ways is an added bonus.