Skip to the page
The language / Strings

Start here01

The language02

Programs and the world03

Kiln04

02 The language / 06

Strings

Text: double quotes, interpolation, text blocks, and the methods that work on it.

Much of what a program does ends up as text for someone to read: a greeting, a report, a line in a log. In Hematite every string is written in double quotes, and values go into it by interpolation, with #{...}, so text and the values in it read in the order they appear. Text that runs over several lines has a form of its own, the text block.

Writing a string

A string goes between double quotes. Inside it, #{...} puts in the value of any expression:

strings.hem
const name = "Ada"
const age = 36

print("Hello, #{name}!")       # prints Hello, Ada!
print("next year: #{age + 1}") # prints next year: 37
  • Strings use double quotes. Single quotes are an error.
  • A string must end on the line where it starts. For a line break inside the text, write \n, or use a text block.
  • Any Unicode is fine inside a string: "blåbær 🦀". (Names are limited to plain letters, digits and _; the text in strings isn't.)
  • # inside a string is plain text, not a comment.

Escapes

A backslash, \, starts an escape: a short way to write a character that would otherwise end the string, start an interpolation, or be hard to type.

WriteTo get
\nline break
\ttab
\\\
\""
\## (so "\#{x}" is the text #{x})
\u{e9}the Unicode character U+00E9 (é); 1 to 6 hex digits

Any other \ sequence is an error, so a stray backslash never slips through unnoticed.

Interpolation: #{...}

#{expression} inside a string inserts the expression's value as text. The expression can be anything that gives a value: a variable, arithmetic, a comparison, even a one-line if ... then ... else:

interpolation.hem
const name = "Ada"
const age = 36
const ok = true

print("#{name} is #{age} years old")          # prints Ada is 36 years old
print("half: #{age / 2}")                     # prints half: 18.0
print("adult: #{age >= 18}")                  # prints adult: true
print("label: #{if ok then "yes" else "no"}") # prints label: yes

Values appear exactly as print prints them, which is why age / 2 shows as 18.0: / always gives a Float.

A value that might be nil must be checked first, and nil itself can't go into a string. Text is for people, and "nil" showing up in it is a bug.

The expression must fit on the line, and can't contain a comment or an assignment. #{} with nothing inside is an error.

Text blocks: """

Text of several lines, like SQL or a message, goes between """ and """. The text inside can be indented along with the code around it, and the indentation is taken off again:

text-blocks.hem
const table = "links"
const sql = """
  SELECT "title"
  FROM #{table}
    WHERE read = 0
  """

print(sql) # prints SELECT "title"
# prints FROM links
# prints   WHERE read = 0

Here is how the block above becomes text:

  • The opening """ ends its line, and the text starts on the next.
  • Every line loses the indentation the closing """ has, here two spaces. A line that starts further left than that is an error; one that's further in, like WHERE, keeps what's past it.
  • The line break before the closing """ isn't part of the text, so sql ends with 0.
  • The closing """ starts its own line, and code may go on after it, as in """, as: [String]) at the end of a call.

Escapes and #{...} work as in any string, and each #{...} ends on its line. A " on its own is plain text, which is handy for SQL like the "title" above.

One line of text always goes in "...": """one line""" is an error.

Joining strings

+ joins two strings. Joining a string with anything else, such as an Int, is an error; put the value in with interpolation instead:

joining-strings.hem
const age = 36

print("foo" + "bar") # prints foobar
print("age: " + age) # error: can't apply '+' to String and Int
print("age: #{age}") # prints age: 36

String length

.length gives the number of characters in a string: "blåbær".length is 6. It is a value, so it has no parentheses (see parentheses). The methods strings have, such as upcase(), are listed with the other built-in methods.