January 2020
Intermediate to advanced
532 pages
13h 31m
English
Type II piracy can be mitigated by creating your own types and using them in the function argument. In this case, perhaps we should consider creating a wrapper type to hold the string and using this new type for dispatch:
module MyModule export @str_str import Base: +, show struct MyString value::AbstractString end macro str_str(s::AbstractString) MyString(s) end show(io::IO, s::MyString) = print(io, s.value) (+)(s::MyString, n::Number) = MyString(s.value * string(n)) (+)(n::Number, s::MyString) = MyString(string(n) * s.value) (+)(s::MyString, t::MyString) = MyString(s.value * t.value)end
Here, we have redefined the module with a new MyString type that holds a string. Then, we can still extend the + function to concatenate ...
Read now
Unlock full access