ddonche/goblin-lang
0.46.24
1
0
docs reference
[[concatenation]]

Concatenation


Concatenation joins two values into one.


Overview

Goblin has two concatenation operators: + and ++.

+ joins with no space:

"Dave" + "Mustaine"    /// -> "DaveMustaine"
"Dave" + "Mustaine"    /// -> "DaveMustaine"

++ joins with a space:

"Dave" ++ "Mustaine"   /// -> "Dave Mustaine"
"Dave" ++ "Mustaine"   /// -> "Dave Mustaine"

The space in ++ implies the space in the output.


With Variables

first | "Dave"
last  | "Mustaine"

first + last     /// -> "DaveMustaine"
first ++ last    /// -> "Dave Mustaine"
first | "Dave"
last  | "Mustaine"

first + last     /// -> "DaveMustaine"
first ++ last    /// -> "Dave Mustaine"


With Arrays

+ merges two arrays into one:

a | [1, 2, 3]
b | [4, 5, 6]
c | a + b
/// -> [1, 2, 3, 4, 5, 6]
a | [1, 2, 3]
b | [4, 5, 6]
c | a + b
/// -> [1, 2, 3, 4, 5, 6]

++ stringifies both arrays and joins with a space:

colors ++ shades
/// -> "[blue, red, white] [tan, gray, mauve]"
colors ++ shades
/// -> "[blue, red, white] [tan, gray, mauve]"


In Practice

A common pattern is building sentences from parts:

final | :join(words, " ")
para  | final + ". "
final | :join(words, " ")
para  | final + ". "

Or assembling output across multiple lines:

say "Hello" ++ name ++ "welcome to Goblin."
say "Hello" ++ name ++ "welcome to Goblin."