I stumbled across two situations where functions accept symbols as arguments, and wonder why they are designed that way. What advantages are there to using symbols? When should I write functions that accept symbols instead of strings or booleans? Here are the examples:
- In the Debugger, to break on error:
Debugger.break_on(:error)
Why not write break_on
to accept strings, for example, Debugger.break_on("error")
?
- To get the labels for the MNIST test data set:
Flux.Data.MNIST.labels(:test)
The labels can be either test or train, so why not use something like Flux.Data.MNIST.labels(test = true)
?
@StefanKarpinski’s answer to “what is a Symbol?” answers this question for DataFrames, but I don’t see how the reasoning applies in the cases above. The docs on metaprogramming explain what is happening, and some of the cool things you can do with it, but not why you would want to for these use cases.