Why must a constant's String be written with double quotes, and why is "C:\users" an error?

← Constants and Immutability · Ref: Q1394

A constant, a constraint, a text's language (defines text for "de") and a service parameter's name (HEADER "X-Token") are all read at compile time, so each must be a plain "..." literal. A backtick String - interpolated or not - is not a compile-time literal, and is E07902. (Interpolation belongs in a function or a local variable, where it is evaluated.)

In any String, a backslash-u starts a unicode escape and must be followed by exactly four hex digits (0-9, a-f, A-F): "\u00e9" is an e with an acute accent. Anything else - "\u+041", a too-short "\u004", or the 'u' of a Windows path like "C:\users" - is E07903. To write a backslash followed by a 'u', escape the backslash: "C:\\users".

See Q43 for escaping in interpolated Strings and Q909 for $ in backtick Strings.

Example

defines module qa.constants.plain.literals

  defines constant

    GREETING <- "Welcome"

  defines function

    backslashPath() as pure
      <- rtn as String: "C:\\users\\steve"

  defines program

    PlainLiteralsDemo()
      stdout <- Stdout()
      stdout.println(GREETING)
      stdout.println(backslashPath())

Common mistakes

E07902 — A constant is read at compile time, and a backtick String is not a compile-time literal - interpolated or not. Before E07902 its raw source text, backticks included, became the constant's value. Write the constant with double quotes; build interpolated text in a function or a local variable instead.

Incorrect:

    GREETING <- `Welcome`

Correct:

    GREETING <- "Welcome"

E07903 — A backslash-u must be followed by exactly four hex digits; 'sers' is not, so the String is rejected rather than guessed at. Escape the backslash - two backslashes - to write a backslash followed by a plain 'u'.

Incorrect:

      <- rtn as String: "C:\users\steve"

Correct:

      <- rtn as String: "C:\\users\\steve"
Other ways to ask this
  • What does E07902 'a plain "..." String literal is required here' mean?
  • Can a constant use a backtick String?
  • What does E07903 'is not a unicode escape' mean?
  • How do I write a backslash followed by a u in an EK9 String?

Coming from another language?

Java: a malformed \u in source is an 'illegal unicode escape' compile error - even in a comment. Kotlin: const val needs a compile-time constant, but string templates are allowed and folded. Python and JavaScript: no compile-time constants. EK9: constants are literals, written only as "...", and a malformed escape is named for what it is.

Keywords: backtick, backslash, String, E07902, E07903, escape, literal, double quote, unicode, constant