# Multiple dispatch by String alias

**URL:** https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400
**Category:** New to Julia
**Tags:** question, strings, type, dispatch
**Created:** [July 20, 2020, 7:57pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400 "2020-07-20T19:57:07Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Boo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boo/32/16341_2.png) [@Boo](https://discourse.julialang.org/u/Boo)
#### Post date: [July 20, 2020, 7:57pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/1 "2020-07-20T19:57:07Z")

</div>

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:

```julia
alias MyString = String # Pseudocode

f(str::String) = str

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

```

Thank you.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 20, 2020, 8:03pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/2 "2020-07-20T20:03:41Z")

</div>

What you want isn’t an alias but a wrapper

```
struct Mystring<:AbstractString
   data::String
end

```

Then you can define all the methods you need easily.

---

<div class="post-metadata">

### Author: ![Boo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boo/32/16341_2.png) [@Boo](https://discourse.julialang.org/u/Boo)
#### Post date: [July 20, 2020, 8:23pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/3 "2020-07-20T20:23:30Z")

</div>

@Oscar_Smith It is not that easy according to [https://github.com/JuliaLang/julia/tree/master/base/strings](https://github.com/JuliaLang/julia/tree/master/base/strings)

I really need to redefine just `Base.show` 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?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 20, 2020, 8:25pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/4 "2020-07-20T20:25:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)
#### Post date: [July 20, 2020, 10:33pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/5 "2020-07-20T22:33:11Z")

</div>

Possibly useful macro:

> [@Composition and inheritance: the Julian way](https://discourse.julialang.org/t/composition-and-inheritance-the-julian-way/11231/3):
>
> I’ve found the @forward macro from Lazy.jl quite useful for this purpose: [https://github.com/MikeInnes/Lazy.jl/blob/76dab7d30863e8375fc96b2444fdb2ead369bf4c/src/macros.jl#L248-L271](https://github.com/MikeInnes/Lazy.jl/blob/76dab7d30863e8375fc96b2444fdb2ead369bf4c/src/macros.jl#L248-L271) Essentially, you can define: struct Person name::String end name(p::Person) = person.name struct Citizen person::Person nationality::String end @forward Citizen.person name which will automatically make name(c::Citizen) = name(c.person)

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 21, 2020, 3:18pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/6 "2020-07-21T15:18:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Boo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/boo/32/16341_2.png) [@Boo](https://discourse.julialang.org/u/Boo)
#### Post date: [July 21, 2020, 3:35pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/7 "2020-07-21T15:35:35Z")

</div>

@hendri54

I want to make a small handy library for ANSI color support in terminal for my pet-project.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 21, 2020, 6:51pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/8 "2020-07-21T18:51:41Z")

</div>

So dynamic dispatch would be fine (performance wise)?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 21, 2020, 7:05pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/9 "2020-07-21T19:05:46Z")

</div>

> [@Boo](#):
>
> I really need to redefine just `Base.show` and inherit the other methods from `String` .

Sounds similar to [https://github.com/stevengj/LaTeXStrings.jl/blob/master/src/LaTeXStrings.jl](https://github.com/stevengj/LaTeXStrings.jl/blob/master/src/LaTeXStrings.jl) so you might look at how it’s done there.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 21, 2020, 8:01pm UTC](https://discourse.julialang.org/t/multiple-dispatch-by-string-alias/43400/10 "2020-07-21T20:01:07Z")

</div>

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