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 !
![No description](https://answer-overflow-discord-attachments.s3.amazonaws.com/1232012608840925376/image.png)
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 time![No description](https://answer-overflow-discord-attachments.s3.amazonaws.com/1232013161909981362/image.png)
![No description](https://answer-overflow-discord-attachments.s3.amazonaws.com/1232013162203713639/image.png)
I would just use it in this one place
this is my actual code
![No description](https://answer-overflow-discord-attachments.s3.amazonaws.com/1232013406136041492/image.png)
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
![No description](https://answer-overflow-discord-attachments.s3.amazonaws.com/1232013542631280691/image.png)
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