Is there a way to get the name of a custom type constructor as a string? For example, suppose I have this custom type:
type User = Regular String Int | Visitor String
I want a function
toString such that:
toString Regular == "Regular" toString Visitor == "Visitor"
I’m interested in using this to build an API for a port that is similar to the API for Elm’s builtin commands. Usually, they take a parameter that is a function which takes the result and turns it into a message. The first parameter of the
Http.send function is an example of this:
Http.send NewBook bookRequest
The closest I’ve gotten with my port API is to have the caller provide the name of the constructor as a string rather than passing the constructor itself. Here’s an example where
SetDate is the message it wants to get back when the response is ready:
getParsedDate : String -> String -> Cmd msg type DateMsg = SetDate Int -- Example invocation of `getParsedDate` update = ... (model, getParsedDate "SetDate" someDateString) ...
If you want more details, you can see a full example at https://ellie-app.com/44hQBb9gqK2a1.
My goal is to be able to write the invocation like this instead:
update = ... (model, getParsedDate SetDate someDateString) ...