Skip to content

Commit

Permalink
RFC: Document the rationale behind * for strings (#19643)
Browse files Browse the repository at this point in the history
* Document the rationale behind * for strings

[ci skip]

* Use jldoctest for string concatenation code blocks
  • Loading branch information
ararslan authored and stevengj committed Dec 20, 2016
1 parent 4edf497 commit 194e4c9
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions doc/src/manual/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ further discussion of UTF-8 encoding issues, see the section below on [byte arra
The [`transcode()`](@ref) function is provided to convert data between the various UTF-xx encodings,
primarily for working with external data and libraries.

## [Interpolation](@id string-interpolation)
## Concatenation

One of the most common and useful string operations is concatenation:

```julia
```jldoctest stringconcat
julia> greet = "Hello"
"Hello"
Expand All @@ -333,11 +333,37 @@ julia> string(greet, ", ", whom, ".\n")
"Hello, world.\n"
```

Constructing strings like this can become a bit cumbersome, however. To reduce the need for these
verbose calls to [`string()`](@ref), Julia allows interpolation into string literals using `$`,
as in Perl:
Julia also provides `*` for string concatenation:

```julia
```jldoctest stringconcat
julia> greet * ", " * whom * ".\n"
"Hello, world.\n"
```

While `*` may seem like a surprising choice to users of languages that provide `+` for string
concatenation, this use of `*` has precedent in mathematics, particularly in abstract algebra.

In mathematics, `+` usually denotes a *commutative* operation, where the order of the operands does
not matter. An example of this is matrix addition, where `A + B == B + A` for any matrices `A` and `B`
that have the same shape. In contrast, `*` typically denotes a *noncommutative* operation, where the
order of the operands *does* matter. An example of this is matrix multiplication, where in general
`A * B != B * A`. As with matrix multiplication, string concatenation is noncommutative:
`greet * whom != whom * greet`. As such, `*` is a more natural choice for an infix string concatenation
operator, consistent with common mathematical use.

More precisely, the set of all finite-length strings *S* together with the string concatenation operator
`*` forms a [free monoid](https://en.wikipedia.org/wiki/Free_monoid) (*S*, `*`). The identity element
of this set is the empty string, `""`. Whenever a free monoid is not commutative, the operation is
typically represented as `\cdot`, `*`, or a similar symbol, rather than `+`, which as stated usually
implies commutativity.

## [Interpolation](@id string-interpolation)

Constructing strings using concatenation can become a bit cumbersome, however. To reduce the need for these
verbose calls to [`string()`](@ref) or repeated multiplications, Julia allows interpolation into string literals
using `$`, as in Perl:

```jldoctest stringconcat
julia> "$greet, $whom.\n"
"Hello, world.\n"
```
Expand Down

2 comments on commit 194e4c9

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels

Please sign in to comment.