mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-10-31 03:16:40 +08:00
Adds the ability to define enums inside ChaiScript with syntax:
enum Color { Red, Green, Blue }
enum Priority { Low = 10, Medium = 20, High = 30 }
Enum values are strongly typed Dynamic_Objects accessed via :: syntax
(e.g. Color::Red). A validating constructor from int is registered that
rejects values outside the defined range. Functions declared with an enum
parameter type (e.g. def fun(Color val)) correctly reject plain integers,
enforcing type safety at the dispatch level.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
60 lines
1.4 KiB
ChaiScript
60 lines
1.4 KiB
ChaiScript
// Basic enum definition
|
|
enum Color { Red, Green, Blue }
|
|
|
|
// Access via :: syntax
|
|
auto r = Color::Red
|
|
auto g = Color::Green
|
|
auto b = Color::Blue
|
|
|
|
// Equality and inequality
|
|
assert_true(Color::Red == Color::Red)
|
|
assert_false(Color::Red == Color::Green)
|
|
assert_true(Color::Red != Color::Green)
|
|
assert_false(Color::Red != Color::Red)
|
|
|
|
// Construction from valid int
|
|
auto c = Color(1)
|
|
assert_true(c == Color::Green)
|
|
|
|
// Construction from invalid int throws
|
|
try {
|
|
Color(52)
|
|
assert_true(false)
|
|
} catch(e) {
|
|
// expected
|
|
}
|
|
|
|
// Strong typing: function with typed parameter
|
|
def takes_color(Color val) { val }
|
|
takes_color(Color::Red)
|
|
takes_color(Color::Green)
|
|
takes_color(Color(2))
|
|
|
|
// Cannot pass int where Color is expected
|
|
try {
|
|
takes_color(52)
|
|
assert_true(false)
|
|
} catch(e) {
|
|
// expected: dispatch error
|
|
}
|
|
|
|
// to_int accessor
|
|
assert_equal(0, Color::Red.to_int())
|
|
assert_equal(1, Color::Green.to_int())
|
|
assert_equal(2, Color::Blue.to_int())
|
|
|
|
// Enum with explicit values
|
|
enum Priority { Low = 10, Medium = 20, High = 30 }
|
|
assert_equal(10, Priority::Low.to_int())
|
|
assert_equal(20, Priority::Medium.to_int())
|
|
assert_equal(30, Priority::High.to_int())
|
|
|
|
auto p = Priority(20)
|
|
assert_true(p == Priority::Medium)
|
|
|
|
// Mixed auto and explicit values
|
|
enum Status { Pending, Active = 5, Done }
|
|
assert_equal(0, Status::Pending.to_int())
|
|
assert_equal(5, Status::Active.to_int())
|
|
assert_equal(6, Status::Done.to_int())
|