Skip to content

Support automatic string‑based selectors (inline select) #332

@azalessky

Description

@azalessky

Motivation

Currently, when defining a selector with param=room in JSON:

{
  "notification": {
    "lessonBody(param=room)": {
      "empty": "$time",
      "other": "$time • room $room"
    }
  }
}

Slang generates two separate methods (empty(...) and other(...)). The developer must manually decide which one to call in code.

It would be useful to have an inline selector feature, similar to ICU select, where the generator produces one method (e.g. lessonBody(...)) and automatically chooses the correct branch based on the parameter value.

Developer Experience

Proposed syntax:

{
  "notification": {
    "lessonBody(select, param=room)": {
      "empty": "$time",
      "other": "$time • room $room"
    }
  }
}

Expected generated API:

String lessonBody({required DateTime time, required String room});

Usage:

final body = t.notification.lessonBody(time: lesson.start, room: lesson.room);

If room is empty → returns $time.
If room is non‑empty → returns $time • room $room.

Benefits

  • Cleaner developer experience.
  • Less duplication in generated code.
  • More intuitive for those familiar with ICU select.
  • Consistent handling of string‑based conditions (empty/non‑empty, status values, gender, etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions