# Is it possible to define '+' operator for string as '\*' do for them?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835>\
**Category:** New to Julia\
**Tags:** question, strings, operator\
**Created:** [August 25, 2025, 11:58am UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835 "2025-08-25T11:58:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 25, 2025, 11:58am UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/1 "2025-08-25T11:58:47Z")

</div>

I’d like to use ‘+’ operator for string concatenation.  
but it is ‘\*’ in Julia.

If possible I’d like to define ‘+’ as alias of ‘\*’ if opland are strings.  
because some insufficient definition cause some side effect.

Thanks in advance!!

---

<div class="post-metadata">

**Author:** ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)\
**Post date:** [August 25, 2025, 12:18pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/2 "2025-08-25T12:18:39Z")

</div>

No, that would be type piracy. Do not do this!

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [August 25, 2025, 12:28pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/3 "2025-08-25T12:28:21Z")

</div>

Of course, you can:

```julia
Base.:+(a::String, b::String)=a*b

```

But there is a [clear mathematical reason](https://docs.julialang.org/en/v1/manual/strings/#man-concatenation) for the choice in Julia: generally, “+” is considered to be always commutative, while “\*” (e.g. for matrices) may be not commutative, and string concatenation is clearly not commutative. Also, yes, this would be [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#avoid-type-piracy) which may render your code harmful if it is used by others.

---

<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:** [August 25, 2025, 12:56pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/4 "2025-08-25T12:56:32Z")

</div>

See also the Julia FAQ: [Why does Julia use `*` for string concatenation? Why not `+` or something else?](https://docs.julialang.org/en/v1/manual/faq/#Why-does-Julia-use-*-for-string-concatenation?-Why-not-or-something-else?)

In general, when you learn a new language, you should try to learn the idiomatic “spelling” of things in that language. Trying to write code in Julia that superficially resembles some other language (e.g. Python) is just going to get you into trouble — even if there are no _technical_ issues (ala type piracy), your code won’t fit nicely in to the surrounding ecosystem.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [August 25, 2025, 9:11pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/5 "2025-08-25T21:11:36Z")

</div>

It is however possible to create your own string type with whatever properties you like, without breaking any norms like “type piracy”:

```julia-auto
struct MyString <: AbstractString
    s::String
end

macro s_str(e)
    :(MyString($e))
end

Base.getindex(s::MyString, arg::Int) = getindex(s.s, arg)
Base.iterate(s::MyString, state::Int=1) = iterate(s.s, state)
Base.:+(s1::MyString, s2::MyString) = MyString(s1*s2)
Base.:*(n::Integer, s::MyString) = MyString(s.s^n)

julia> s1 = s"string1"
"string1" # a MyString

julia> s2 = s"string2"
"string2" # a MyString

julia> s1 + s2
"string1string2" # also a MyString

julia> 4s1
"string1string1string1string1"

# but, while many operations on Strings work for MyString, they may still return String
# not MyString, unless you handle it specially.

julia> s3 = join((s1,s2), ", ")
"string1, string2"

julia> typeof(s3)
String
```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [August 25, 2025, 11:53pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/6 "2025-08-25T23:53:43Z")

</div>

It’s even easier to create your own `+`! No piracy, and you don’t need to forward all the other string methods.

```julia
julia> a::String + b::String = a * b # may need to restart the session for this line to work
+ (generic function with 1 method)

julia> "a" + "b"
"ab"

julia> 2 + 2
ERROR: MethodError: no method matching +(::Int64, ::Int64)
You may have intended to import Base.:+
The function `+` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  +(::String, ::String)
   @ Main REPL[1]:1

Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> a + b = Base.:+(a, b) # fall back to the Base version
+ (generic function with 2 methods)

julia> 2+2
4

```

It’s a nice trick for designing DSLs.

In any case, “just use `*`” is the correct answer of course.

---

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 26, 2025, 1:11pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/7 "2025-08-26T13:11:24Z")

</div>

Thank you j-fu for your code and your advice.  
I found that your code works well for me.

but “+” looks more natural operator for string concatenation I suppose…

---

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 26, 2025, 1:13pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/8 "2025-08-26T13:13:19Z")

</div>

Thank you for your suggestion cstjean.  
I learned a lot from your code.

I appreciate it!!

---

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 26, 2025, 1:14pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/9 "2025-08-26T13:14:41Z")

</div>

Well, but ‘\*’ looks strange for me…  
thanks anyway

---

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 26, 2025, 1:17pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/10 "2025-08-26T13:17:26Z")

</div>

I understand your point.  
thank you for your information.  
but I am going to use my code for internal use only.

---

<div class="post-metadata">

**Author:** ![psalm1750](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psalm1750/32/210992_2.png) [@psalm1750](https://discourse.julialang.org/u/psalm1750)\
**Post date:** [August 26, 2025, 1:19pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/11 "2025-08-26T13:19:44Z")

</div>

thanks sgaure,  
maybe you are genius!  
Although I will use j-fu method for now, I might use your method in some other issues.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 26, 2025, 1:26pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/12 "2025-08-26T13:26:42Z")

</div>

Take also a look [at this thread](https://discourse.julialang.org/t/introduce-as-the-concatenation-operator/37418/61) and the tiny Julia package PlusPlus.jl with just three lines of code.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [August 26, 2025, 1:29pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/13 "2025-08-26T13:29:30Z")

</div>

Another thing is that string concatenation may not be too useful in any language.

Note you can do e.g.:

```julia-auto
println(a, b) # and/or use string interpolation

```

it will be faster than doing concatenation first explicitly: `c = a * b` first, or `c = String(a, b)` another possibility if you do not like `*`, and it’s similar syntax to `println`, but result can be used generally, and a and b can be strings, or e.g. integers, unlike for `*`.

Welcome to the Julia community!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 29, 2025, 1:48pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-operator-for-string-as-do-for-them/131835/14 "2025-08-29T13:48:04Z")

</div>

59 posts were split to a new topic: [The strangeness (or not) of \* as string concatenation](https://discourse.julialang.org/t/the-strangeness-or-not-of-as-string-concatenation/131943)
