CH02-L02 · foundation

Strings, Unicode, and bytes

Build text containing diacritics and distinguish text from its byte representation.

General CELRuns hereDocumented
What will I learn?

You can choose string for text and bytes only when an application actually needs bytes.

Where will I try it?

Directly below, in the browser playground.

What do I do?

Run both modes, compare where their values come from, then complete the task.

Try CEL · browser pilot

Try both ways to supply values

Define local values inside one CEL expression, or provide variables as JSON input. Everything runs in your browser.

CEL pilot subset
One self-contained CEL expressioncel.bind() gives a local name to a value and returns the result of the final expression.

cel.bind()cel.bind() is an optional official extension. It is not part of the core CEL language and may not be enabled by every host application.

Tip: press Ctrl/⌘ + Enter to run.
Result

Press Run CEL to evaluate the active example.

Pilot scope: literals, explicit conversions, local cel.bind() variables, JSON variables, lists, maps, presence, collection macros, core string checks, time values, arithmetic, comparisons, logic, and the conditional operator.

What you will do

You will run the prepared expression in the playground above, change one input at a time, and explain the result. Start in CEL only, then repeat the same rule in CEL + JSON.

How it works

A CEL string represents Unicode text. A bytes value represents raw bytes and is a different type; it is not a second spelling of text. Concatenation with + accepts strings on both sides. Characters such as Ľ and 👋 remain part of the string without special application logic.

Read the prepared example

The CEL + JSON rule is:

greeting + ', ' + name + ' 👋'

Its input is:

{
  "greeting": "Hello",
  "name": "Ľubica"
}

The CEL only tab contains the values and the rule in one expression. The enabled cel.bind() extension keeps each name local to its final argument.

Evaluation step by step

  1. greeting supplies the first text fragment.
  2. Two more string fragments and name are joined from left to right.
  3. The result is one Unicode string.

Your task

  1. Run both modes.
  2. Replace the name with text containing your own diacritics.
  3. In CEL only, run type(bytes('CEL')) == bytes.

Expected observation: The greeting preserves Unicode characters; the type check for bytes() returns true.

Common mistake

Do not use bytes merely because text will eventually be stored or transmitted. Use it only when the host API expects a byte sequence.

Show the explanation

No. They are different types and require an explicit, supported conversion.

Knowledge check

Can a bytes value be concatenated directly with a string?

Key takeaway

Use string for human-readable text. Treat bytes as a separate data type with a separate purpose.

Sources

  • CEL-DEV — official CEL overview.
  • CEL-LANG — official CEL language definition.
  • CEL-GO-BIND — official documentation for the optional cel.bind() extension used by the self-contained tab.