การเรนเดอร์th-TH

ordo-conversio

Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.

An ลำดับ converts to its number and name, and a number converts back to a member.

Syntax: <ordo-value> ↦ int | <ordo-value> ↦ string | <int-expression> ↦ <Ordo> [⊥ <default>]

Category#

conversion

Examples#

radix/corpus/ordo/ordo-conversio.fab (canonical · conversio)#

An ลำดับ converts to its number and name, and a number converts back to a member.

# =============================================================================
# ordo-conversio — An ลำดับ converts to its number and name, and back.
# =============================================================================
#
# What this teaches:
#   • `member ↦ int` gives the member's number. A member without `= n`
#     takes the previous member's number plus one; the first defaults to 0.
#   • `member ↦ string` gives the member's name. It cannot fail.
#   • `n ↦ Ordo` finds the first member with that number. It can fail, so
#     write a `⊥` default for values that match no member.
#
# Common mistakes:
#   • Writing methods on the ลำดับ to convert it — an ลำดับ is data only;
#     these three conversions are built in.
#   • An ลำดับ with a string-valued member has no number, so it converts
#     only to string.
#
# See also: ลำดับ, ↦, ⊥
# =============================================================================

enum Color {
    red,
    green,
    blue
}

enum Status {
    pending = 10,
    active,
    done = -3,
    archived
}

main {
    # ลำดับ ↦ int: declared or implicit numbers
    print Color.green ↦ int
    print Status.active ↦ int
    print Status.archived ↦ int

    # ลำดับ ↦ string: the member name
    print Color.blue ↦ string
    print Status.done ↦ string

    # int ↦ ลำดับ: the matching member, or the ⊥ default
    const Color found ← 2 ↦ Color ⊥ Color.red
    print found ↦ string
    const Color missing ← 9 ↦ Color ⊥ Color.red
    print missing ↦ string
    const Status negative ← -2 ↦ Status ⊥ Status.pending
    print negative ↦ string
}

Expected output:

1
11
-2
blue
done
blue
red
archived