Write me a Booking class with a SINGLE-dollar $ operator that returns a human-readable reservation line — no JSON, just plain text.

← Operators and Expressions · Ref: Q1257

The SINGLE-dollar $ operator produces plain text. This file shows a Booking class where $ returns a formatted reservation line. NO $$ is used anywhere — this is a pure single-dollar example.

BOOKING CLASS WITH $ OVERRIDE

  Booking
    reference as String: String()
    guestName as String: String()
    nights as Integer: 0
    Booking()
      ->
        reference as String
        guestName as String
        nights as Integer
      this.reference :=: reference
      this.guestName :=: guestName
      this.nights :=: nights
    operator $ as pure
      <- rtn as String: `Booking ${reference} for ${guestName}${nights} nights`
    override operator ? as pure
      <- rtn as Boolean: reference?

USAGE

  booking <- Booking("BK-2024-0817", "Alice", 3)
  line <- $booking
  stdout.println(line)
  // Output: Booking BK-2024-0817 for Alice — 3 nights

KEY POINT

The return type of $ is always String. The return type of $$ is JSON. They are DIFFERENT operators. This example uses ONLY $ — there is no $$ anywhere in the code.

See Q1255 for another $-only example (Product record). See Q1256 for a $$-only example (Customer). See Q887 for the operator contracts.

Example

defines module qa.operators.dollarstringbooking

  defines class

    Booking
      reference as String: String()
      guestName as String: String()
      nights as Integer: 0

      Booking()
        ->
          reference as String
          guestName as String
          nights as Integer
        this.reference :=: reference
        this.guestName :=: guestName
        this.nights :=: nights

      operator $ as pure
        <- rtn as String: `Booking ${reference} for ${guestName}${nights} nights`

      override operator ? as pure
        <- rtn as Boolean: reference?

  defines program

    DollarStringBookingDemo()
      stdout <- Stdout()

      booking <- Booking("BK-2024-0817", "Alice", 3)
      line <- $booking
      stdout.println(line)

Common mistakes

E07580 — $ must return String, not JSON. JSON is the return type for $$. See ek9 -h E07580 for details.

Incorrect:

operator $ as pure
        <- rtn as JSON: JSON()

Correct:

      operator $ as pure
        <- rtn as String: `Booking ${reference} for ${guestName}${nights} nights`
Other ways to ask this
  • Show me another $ operator example on a custom class, not a record.
  • Give me a $-only Booking example — no $$ involved.
  • Override operator $ on a Booking class to produce a readable String line.
  • Create a worked example of $ (plain text only) on a reservation class.

Coming from another language?

Java: toString() override on the class. Kotlin: override fun toString(). Python: __str__. Rust: impl Display for Booking. C#: override ToString(). EK9: 'operator $ as pure' returning String — plain text, never JSON.

Keywords: reservation, Booking, class, plain text, dollar, $, string operator