January 2020
Intermediate to advanced
532 pages
13h 31m
English
In Julia, function calls are dispatched according to the type of the arguments when the function is called. For a quick introduction to this mechanism, please refer to Chapter 3, Designing Functions and Interfaces.
Let's recall the use case that we presented earlier in this chapter about a command processor function. With a naive implementation, we have a large if-then-else block that dispatch to different functions according to the command string. Let's try to implement the same feature using singleton types.
For each command, we can define a function that takes a singleton type. For example, the signatures of the functions for the Open and Close events are as follows:
function process_command(::Val{:open}, ...Read now
Unlock full access