Call a function based on its type?



  • @error said in Call a function based on its type?:

    @hungrier said in Call a function based on its type?:

    @topspin said in Call a function based on its type?:

    @error said in Call a function based on its type?:

    @topspin said in Call a function based on its type?:

    This SO guy says you can use the dynamic keyword for this?

    :facepalm: So did I, but I was 100% trolleybusing.

    I’m not sure what that means (because I don’t know what that code actually does).

    I just assumed it means you can provide overloads of the function with different signatures (one for each derived class you’re trying to handle) and it does the dynamic dispatch for you. But it sounds like you think it’s a really WTFy way to do things?

    I haven't looked at the SO page, and am also responding to a couple days old post so likely :hanzo:'d, but in my C# experience I've only ever seen dynamic used for deserializing JSON objects. You would use Json.Parse (or whatever the syntax is) and assign the result to a dynamic, then write the rest of the code like normal, without any formal type safety other than pinky-promising that whatever .property you write will be there by the time the object is parsed.

    This is about the only reason I use it. When working with a web service response, you don't "really" have type-safety anyway because it could send you anything. Having to write .GetProperty( foo ).GetProperty( bar ) or define a DTO is a lot of effort for little benefit.


    Filed under: Using a DTO gives you kinda sorta schema validation... but if you need that level of safety, then actually use a schema FFS.

    JSON schema don't work so well in Swift. I'm currently working with a homegrown API and an iOS app and have to hammer out the "schema" so that I can do proper decoding without having to write it all manually. Swift does it fine (even for nested types) as long as you can define them properly. And I'd much rather have strongly-typed DTOs. Too much playing with JS and its wibbly-wobbly-ness for me recently.


  • 🚽 Regular

    @hungrier said in Call a function based on its type?:

    I haven't looked at the SO page, and am also responding to a couple days old post so likely :hanzo:'d, but in my C# experience I've only ever seen dynamic used for deserializing JSON objects. You would use Json.Parse (or whatever the syntax is) and assign the result to a dynamic, then write the rest of the code like normal, without any formal type safety other than pinky-promising that whatever .property you write will be there by the time the object is parsed.

    IIRC at some point SignalR (not Core) would let you call random methods on its proxy object, which would convert them into the appropriate stringly-typed call.



  • @error Filed under: Using a DTO gives you kinda sorta schema validation... but if you need that level of safety, then actually use a schema FFS.

    Tbh I have used a local domain object (which is pretty much a DTO I guess, although it isn't called that) for exactly this purpose. It gives you a reasonable level of type safety (your unmarshaller will explode if the input is broken) without requiring people to tightly follow an XSD which can be a pain.


  • Considered Harmful

    @bobjanova said in Call a function based on its type?:

    without requiring people to tightly follow an XSD which can be a pain.

    XSDs are as tight or as loose as you write them to be.


    Filed under: But actually producing them is a PITA.