January 2020
Intermediate to advanced
532 pages
13h 31m
English
Now that we are using the enhanced version of the + function, can we rely on the fact that the function will always do exactly what we have made it do? Perhaps surprisingly, the answer is no.
Let's say we have found an open source package called AnotherModule, and we want to use it in our MyModule module. The AnotherModule module happens to do the same kind of type II piracy; however, the author decided to do the right thing—instead of concatenating the arguments as if they are strings, the string argument is parsed into a number and then the two numbers are added together. The code is as follows:
module AnotherModule import Base: +, -, *, / (+)(s::AbstractString, n::T) where T <: Number = parse(T, s) + n
Read now
Unlock full access