4 ms·
A use case is when a sub or method needs to do different things based on the data type of the argument. Example: turning a data structure into a JSON string, s
by perlgeek 1y ago
A use case is when a sub or method needs to do different things based on the data type of the argument.
Example: turning a data structure into a JSON string, solved with multi dispatch: https://github.com/moritz/json/blob/master/lib/JSON/Tiny.pm#L26-L53 https://github.com/moritz/json/blob/master/lib/JSON/Tiny.pm#...
Another common useful example is constructors. A Date class might have constructors that accept
Date(Int, Int where 1..12, Int where 1..31) # Y, M, D
Date(Str where /^\d4-\d2-\d2$/) # YYYY-MM-DD
Date(DateTime) # extract the date of a DateTime object
etc. to make it very intuitive to use.