Selecting an overloaded method based on the runtime type of an object by casting it to dynamic
Is this something I should do ? Is it a common pattern ? Are there any downsides / unexpected behavior I can encounter doing this ?
11 Replies
the downside is throwing away type safety, i'm not even sure this works
if it was me i'd explicitly match certain types to the right methods
It works !
gross
:KEKW:
Why x)
I don't quite get why is it throwing away type safety ?
what if you call this method with a type that doesn't have a method to resolve to?
dynamic
basically exists to ignore any type checking done by the compiler
you shouldn't use it 99% of the timeI would just use it in this one place
this is my actual code
I know Body is always an AstNode
So there will always be a method to resolve to
If it doesn't find a more specific overload it defaults to this one
My issue is :
- I don't want to put the process methods on the AstNode class itself
- I don't want to have to deal with Visitors and what not
My options were that or a bunch of
node is AstNode.Expr.Block block
for all of the subclasses