Multiple dispatch by String alias

Hi there,

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?
For example:

alias MyString = String    # Pseudocode

f(str::String) = str

f(str::MyString) = "prefix" * str

Thank you.

What you want isn’t an alias but a wrapper

struct Mystring<:AbstractString

Then you can define all the methods you need easily.


@Oscar_Smith It is not that easy according to

I really need to redefine just and inherit the other methods from String.

And, how can I find out which methods for String type exist? Not methods(String) for type constructor but methods which use String type?

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.

Possibly useful macro:


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 so you might look at how it’s done there.

1 Like

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).