Is it possible to make alias for
String type some how which inherits all the logic from
String and then make multiple dispatch by this custom type?
alias MyString = String # Pseudocode
f(str::String) = str
f(str::MyString) = "prefix" * str
What you want isn’t an alias but a wrapper
Then you can define all the methods you need easily.
@Oscar_Smith It is not that easy according to https://github.com/JuliaLang/julia/tree/master/base/strings
I really need to redefine just
Base.show and inherit the other methods from
And, how can I find out which methods for
String type exist? Not
methods(String) for type constructor but methods which use
If you define indexing and iterating, most other behaviors should come for free (I think). As long as most methods in base use
AbstractString (which they should), you’ll get the rest of the behavior for free.
I am curious about the context. You have multiple versions of an object that contain only the String as data and want to dispatch based on the “type” of string. Do you mind explaining a little bit how this pattern comes about?
There may be other solutions that avoid writing a wrapper.
I want to make a small handy library for ANSI color support in terminal for my pet-project.
So dynamic dispatch would be fine (performance wise)?
Sounds similar to https://github.com/stevengj/LaTeXStrings.jl/blob/master/src/LaTeXStrings.jl so you might look at how it’s done there.
If you are worried that there may be methods in other code that are defined for
String rather than
AbstractString and that should work for your type of string, then a wrapper would not work.
If you only care about multiple dispatch for new methods that you define, then perhaps traits would be the solution (with dynamic dispatch, though).