Typed application states — enums

This guide describes nominal enums included in the 0.14.0 SDK and matching browser runtime. See the versioned validation report for actual artifact and platform results.

English: model a fixed set of choices

An enum gives application states a declared type. For example, an order can be OrderState.Pending, OrderState.Paid or OrderState.Shipped; an arbitrary String cannot silently become an order state. A separate enum with a case named Paid is still a different type. This helps express data contracts for status, role or workflow values. A Role.Admin value alone does not authenticate a user or grant permission; the application still needs identity and authorization checks.

Run the complete example from the SDK:

threadone run examples/enums.one

The example uses an enum in function parameters/results, a record, an Optional field and a list:

enum OrderState:
  Pending
  Paid
  Shipped

type Order:
  id: String
  state: OrderState
  previous: OrderState?
  history: OrderState[]

fn advance(state: OrderState) -> OrderState:
  if state == OrderState.Pending:
    return OrderState.Paid
  return OrderState.Shipped

fn main():
  order = Order(id: "demo-1", state: OrderState.Pending, previous: none, history: [OrderState.Pending])
  next = advance(order.state)
  print(order.state)
  print(next)
  print(str(OrderState.Shipped))
  print(order.previous or OrderState.Pending)
  print(list_get(order.history, 0) or OrderState.Shipped)
  assert(order.state == OrderState.Pending)
  assert(next == OrderState.Paid)

Expected output:

Pending
Paid
Shipped
Pending
Pending

print and str produce the exact case name, without the enum type prefix. == and != compare values of the same declared enum. Optional enum values use some(value) and none; list_get returns an Optional enum. Fallible results use OrderState!, and a fallible Optional uses OrderState?! with the existing separate error and absence recovery steps. The example is a data-model demonstration: it neither persists orders nor validates payment or shipping events.

The pure example works in the matching browser interpreter. Browser Check accepts the native JSON and HTTP APIs below, but browser Run does not execute JSON, HTTP or other host I/O.

English: exact JSON names

Native JSON encoding uses case-name strings. Decoding accepts the exact declared name and rejects arbitrary strings, wrong case, numbers, null, arrays and tagged objects. Decode errors do not include the supplied enum value.

enum OrderState:
  Pending
  Paid
  Shipped

fn main():
  state = parse_json("\"Paid\"", OrderState) or OrderState.Pending
  print(state)
  print(json_encode(state) or "Encoding failed")

This native example prints Paid, then "Paid". The fallback handles a decoding failure; it is an explicit application choice, not an implicit conversion. req.json(OrderState) applies the existing request content-type/body checks and returns OrderState!. Returning an enum with a route's JSON response shorthand also encodes its exact case name.

JSON input / fieldResult
"Paid" decoded as OrderStateOrderState.Paid
"paid", "Unknown", 1, null, [], {"Paid":true} decoded as OrderStateDecoding error
Required state: OrderState field missing or nullDecoding error
Optional previous: OrderState? field missing or nullnone
history: OrderState[] field containing ["Pending","Paid"]List of the declared enum values
Enum list containing an unknown name, wrong type or more than 1,024 elementsDecoding error

Record decoding keeps the existing duplicate/unknown-field rules, 1 MiB JSON input/output limit and 8 MiB cumulative JSON-list-storage budget. Required list fields still must be supplied. These bounds do not bound total process memory or establish a production security guarantee.

English: syntax and limits

A program has at most 256 enum declarations, separately from the record limit, with 1–64 cases per enum. Enum and case names are ASCII identifiers of at most 64 bytes. Reserved names and the compiler prefix are rejected. Enum type names cannot collide with record or function declarations. Names can be referenced before their declaration, and case names can repeat across distinct enum types.

A local or parameter name may shadow an enum name in expressions. OrderState.Paid selects the enum case only if OrderState is not bound in that scope; otherwise it is ordinary field access and must type-check. Type annotations and parse_json(text, OrderState) still resolve the declared type. Prefer ordinary lowercase local names to keep the distinction clear.

Legacy brace syntax is enum OrderState { Pending; Paid; Shipped; }; do not mix brace and indentation block styles in the same source file. Payloads, explicit numeric discriminants, ordering, arithmetic, implicit String/Int conversion, match expressions, reflection and exhaustive-branch checking are not implemented. Use equality and if/else. Enum lists keep the existing immutable-list limits; Optional/fallible lists, nested lists and Optional list elements remain unsupported. See the specification and release gates.

ภาษาไทย: กำหนดสถานะที่เลือกได้ด้วยชนิดข้อมูล

เอกสารนี้อธิบาย enum ใน SDK 0.14.0 และ browser runtime รุ่นเดียวกัน ดูหลักฐานการติดตั้ง artifact และผลทดสอบแต่ละแพลตฟอร์มในรายงานของรุ่นนี้

Enum ใช้กำหนดชุดค่าที่แน่นอน เช่นสถานะคำสั่งซื้อ OrderState.Pending, OrderState.Paid และ OrderState.Shipped โดย String ทั่วไปจะเปลี่ยนเป็นสถานะนี้โดยอัตโนมัติไม่ได้ แม้ enum อีกชนิดหนึ่งจะมี case ชื่อ Paid เหมือนกันก็ยังถือเป็นคนละชนิด เหมาะสำหรับอธิบายสถานะ บทบาท หรือขั้นตอนงาน แต่ค่าอย่าง Role.Admin ไม่ได้ยืนยันตัวตนหรือให้สิทธิ์ผู้ใช้ แอปยังต้องตรวจตัวตนและสิทธิ์เอง

รัน threadone run examples/enums.one จาก SDK รุ่นเตรียม โค้ดด้านบนใช้ enum เป็น parameter และผลลัพธ์ของฟังก์ชัน เป็นฟิลด์ใน record เป็น Optional และเป็นสมาชิก list ผลลัพธ์เรียงตามบรรทัดคือ Pending, Paid, Shipped, Pending, Pending ตัวอย่างนี้แสดงการใช้ชนิดข้อมูล ไม่ได้บันทึกคำสั่งซื้อหรือตรวจเหตุการณ์ชำระเงินและจัดส่ง

print และ str ให้ชื่อ case ตรงตามที่ประกาศโดยไม่มีชื่อ enum นำหน้า == และ != ใช้เปรียบเทียบ enum ชนิดเดียวกัน Optional ใช้ some(value) หรือ none ส่วน list_get คืน Optional ของ enum ผลลัพธ์ที่อาจผิดพลาดเขียนเป็น OrderState! และผลลัพธ์ที่อาจผิดพลาดหรือไม่มีค่าเขียนเป็น OrderState?! โดยต้องจัดการ error และการไม่มีค่าแยกกันตามกฎเดิม

ตัวอย่างที่ใช้เฉพาะภาษาในหน้านี้รันใน browser interpreter รุ่นเดียวกันได้ ปุ่ม Check ยอมรับ API สำหรับ JSON และ HTTP แต่ปุ่ม Run ในเบราว์เซอร์ยังไม่รัน JSON, HTTP หรือ I/O ของเครื่อง ต้องใช้ native SDK

ภาษาไทย: JSON ใช้ชื่อ case ที่ตรงทุกตัวอักษร

Native JSON เข้ารหัส enum เป็น JSON string ตามชื่อ case ตัวอย่าง JSON ด้านบนแสดง Paid แล้วตามด้วย "Paid" การใช้ fallback เมื่อถอด JSON ไม่สำเร็จเป็นการตัดสินใจที่เขียนไว้ชัดเจนในแอป ไม่ใช่การแปลงชนิดโดยอัตโนมัติ req.json(OrderState) ตรวจ content type และ body ตามกฎเดิมแล้วคืน OrderState! การคืน enum ด้วยรูปแบบตอบ JSON ใน route ก็ใช้ชื่อ case เดียวกัน

JSON หรือฟิลด์ผลลัพธ์
ถอด "Paid" เป็น OrderStateOrderState.Paid
ถอด "paid", "Unknown", 1, null, [] หรือ {"Paid":true} เป็น OrderStateเกิด decoding error
ฟิลด์จำเป็น state: OrderState หายไปหรือเป็น nullเกิด decoding error
ฟิลด์ Optional previous: OrderState? หายไปหรือเป็น nullnone
ฟิลด์ history: OrderState[] รับ ["Pending","Paid"]list ของค่า enum ที่ประกาศไว้
list มีชื่อที่ไม่ได้ประกาศ ชนิดผิด หรือเกิน 1,024 ค่าเกิด decoding error

ข้อความ error ไม่แสดงค่า enum ที่ส่งเข้ามา การถอด record ยังปฏิเสธฟิลด์ซ้ำและฟิลด์ที่ไม่ได้ประกาศ จำกัด JSON ขาเข้าและขาออก 1 MiB และพื้นที่สะสมสำหรับ JSON list 8 MiB ต่อเอกสาร ฟิลด์ list ที่จำเป็นยังต้องส่งมา ขีดจำกัดเหล่านี้ไม่ได้จำกัดหน่วยความจำรวมของโปรเซสหรือยืนยันความปลอดภัยระดับ production

ภาษาไทย: รูปแบบและขีดจำกัด

โปรแกรมหนึ่งประกาศ enum ได้ไม่เกิน 256 ชนิด โดยนับแยกจาก record แต่ละ enum มี 1–64 cases ชื่อ enum และ case ต้องเป็น ASCII identifier ยาวไม่เกิน 64 bytes ห้ามใช้ชื่อสงวนหรือ prefix ของ compiler ชื่อ enum ต้องไม่ชนกับชื่อ record หรือฟังก์ชัน อ้างอิงชนิดก่อนตำแหน่งประกาศได้ และ enum ต่างชนิดใช้ชื่อ case ซ้ำกันได้

ชื่อ local หรือ parameter สามารถบังชื่อ enum ใน expression ได้ OrderState.Paid จะเลือก case ของ enum เมื่อไม่มี binding ชื่อ OrderState ใน scope นั้นเท่านั้น หากมี binding จะอ่านเป็น field access ตามปกติและต้องผ่านการตรวจชนิด ส่วน type annotation และ parse_json(text, OrderState) ยังอ้างถึงชนิดที่ประกาศไว้ การใช้ชื่อตัวแปรตัวเล็กตามปกติช่วยให้อ่านชัดเจน

รูปแบบวงเล็บปีกกาเดิมคือ enum OrderState { Pending; Paid; Shipped; } ห้ามผสมรูปแบบ block แบบปีกกากับ indentation ในไฟล์เดียวกัน รุ่นนี้ยังไม่มี payload, numeric discriminant ที่กำหนดเอง, การเปรียบเทียบลำดับ, เลขคณิต, การแปลงจาก String/Int โดยอัตโนมัติ, match, reflection หรือการตรวจว่าครอบคลุมทุก case ใช้การเปรียบเทียบความเท่ากันและ if/else แทน list ของ enum ยังใช้กฎ immutable list เดิม ไม่รองรับ Optional/fallible list, list ซ้อนโดยตรง หรือสมาชิก Optional ดูรายละเอียดใน ข้อกำหนดภาษา และ เกณฑ์การเผยแพร่