++ as string concatenation operator

237 views
Skip to first unread message

Hans W Borchers

unread,
Jul 21, 2014, 5:51:05 PM7/21/14
to julia...@googlegroups.com
I was interested to define "++" as operator for concatenating strings. I can define "+" for this purpose, but for "++" I get

    julia> function ++(x::String, y::String)
               x
* y
           
end
    ERROR
: syntax: expected "(" in "function" definition

I looked up "//" in Rational.jl, but did not see the difference in how to define this operation.

Steve Kelly

unread,
Jul 21, 2014, 5:57:41 PM7/21/14
to julia...@googlegroups.com
I think the issue is that ++ is not an operator in julia.

Jake Bolewski

unread,
Jul 21, 2014, 6:06:30 PM7/21/14
to julia...@googlegroups.com
It is not, look at julia-parser.scm for the list of operators you are able to define.

Hans W Borchers

unread,
Jul 21, 2014, 7:41:23 PM7/21/14
to julia...@googlegroups.com
> It is not, look at julia-parser.scm for the list of operators you are able to define.

And I thought Julia allows to define operators as users like.
Is there a special reason (inconsistency) why ++ would not not be allowed while // is?
Could this be changed by an appropriate entry in julia-parser.scm (or somewhere else)?

Stefan Karpinski

unread,
Jul 21, 2014, 7:44:44 PM7/21/14
to Julia Users
Julia does not allow user-defined operators. There are just a lot of pre-defined operators. We could add ++ as an operator, but we'd have to decide how it parses and what it's precedence is. In Haskell it's an infix operator used for concatenation. In C it's a prefix and postfix operator that increments things. So there's no single obvious way to parse it.

Ben Arthur

unread,
Jul 22, 2014, 4:55:11 PM7/22/14
to julia...@googlegroups.com
relatedly, is there a thread somewhere that explains why * is used for string concatenation and ^ for string repetition?

Stefan Karpinski

unread,
Jul 22, 2014, 4:58:53 PM7/22/14
to Julia Users
many threads: search "string concatenation"

Spencer Russell

unread,
Jul 22, 2014, 5:19:44 PM7/22/14
to julia...@googlegroups.com
"Why is string concatenation done with * not +"
https://groups.google.com/d/topic/julia-users/nQg_d_n0t1Q/discussion

peace,
s

Stefan Karpinski

unread,
Jul 22, 2014, 7:46:08 PM7/22/14
to Julia Users
Thanks for the link, Spencer. This obviously belongs in the FAQ for now.
Reply all
Reply to author
Forward
0 new messages