From 194e4c96fec2e3ff8203cae1c9d8ffaf3dc3e61c Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Mon, 19 Dec 2016 16:50:35 -0800 Subject: [PATCH] RFC: Document the rationale behind * for strings (#19643) * Document the rationale behind * for strings [ci skip] * Use jldoctest for string concatenation code blocks --- doc/src/manual/strings.md | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/doc/src/manual/strings.md b/doc/src/manual/strings.md index e912d75fdf317..cc4be463dba38 100644 --- a/doc/src/manual/strings.md +++ b/doc/src/manual/strings.md @@ -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" @@ -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" ```