@@ -702,6 +702,39 @@ pub fn register(env: &Environment) {
702702 func : builtin_shadow_price,
703703 } ) ,
704704 ) ;
705+
706+ // Option/Result helper methods (as standalone functions)
707+ env. define (
708+ SmolStr :: new ( "is_some" ) ,
709+ Value :: Builtin ( BuiltinFn {
710+ name : "is_some" ,
711+ func : builtin_is_some,
712+ } ) ,
713+ ) ;
714+
715+ env. define (
716+ SmolStr :: new ( "is_none" ) ,
717+ Value :: Builtin ( BuiltinFn {
718+ name : "is_none" ,
719+ func : builtin_is_none,
720+ } ) ,
721+ ) ;
722+
723+ env. define (
724+ SmolStr :: new ( "is_ok" ) ,
725+ Value :: Builtin ( BuiltinFn {
726+ name : "is_ok" ,
727+ func : builtin_is_ok,
728+ } ) ,
729+ ) ;
730+
731+ env. define (
732+ SmolStr :: new ( "is_err" ) ,
733+ Value :: Builtin ( BuiltinFn {
734+ name : "is_err" ,
735+ func : builtin_is_err,
736+ } ) ,
737+ ) ;
705738}
706739
707740fn builtin_println ( args : & [ Value ] ) -> RuntimeResult < Value > {
@@ -2425,6 +2458,62 @@ fn builtin_shadow_price(args: &[Value]) -> RuntimeResult<Value> {
24252458 Ok ( Value :: Float ( price) )
24262459}
24272460
2461+ /// Check if an Option value is Some.
2462+ /// Usage: is_some(option_value)
2463+ fn builtin_is_some ( args : & [ Value ] ) -> RuntimeResult < Value > {
2464+ if args. len ( ) != 1 {
2465+ return Err ( RuntimeError :: custom ( "is_some requires exactly 1 argument" ) ) ;
2466+ }
2467+
2468+ match & args[ 0 ] {
2469+ Value :: Struct { name, .. } if name. as_str ( ) == "Some" => Ok ( Value :: Bool ( true ) ) ,
2470+ Value :: Struct { name, .. } if name. as_str ( ) == "None" => Ok ( Value :: Bool ( false ) ) ,
2471+ other => Err ( RuntimeError :: type_error ( "Option" , other. type_name ( ) ) ) ,
2472+ }
2473+ }
2474+
2475+ /// Check if an Option value is None.
2476+ /// Usage: is_none(option_value)
2477+ fn builtin_is_none ( args : & [ Value ] ) -> RuntimeResult < Value > {
2478+ if args. len ( ) != 1 {
2479+ return Err ( RuntimeError :: custom ( "is_none requires exactly 1 argument" ) ) ;
2480+ }
2481+
2482+ match & args[ 0 ] {
2483+ Value :: Struct { name, .. } if name. as_str ( ) == "None" => Ok ( Value :: Bool ( true ) ) ,
2484+ Value :: Struct { name, .. } if name. as_str ( ) == "Some" => Ok ( Value :: Bool ( false ) ) ,
2485+ other => Err ( RuntimeError :: type_error ( "Option" , other. type_name ( ) ) ) ,
2486+ }
2487+ }
2488+
2489+ /// Check if a Result value is Ok.
2490+ /// Usage: is_ok(result_value)
2491+ fn builtin_is_ok ( args : & [ Value ] ) -> RuntimeResult < Value > {
2492+ if args. len ( ) != 1 {
2493+ return Err ( RuntimeError :: custom ( "is_ok requires exactly 1 argument" ) ) ;
2494+ }
2495+
2496+ match & args[ 0 ] {
2497+ Value :: Struct { name, .. } if name. as_str ( ) == "Ok" => Ok ( Value :: Bool ( true ) ) ,
2498+ Value :: Struct { name, .. } if name. as_str ( ) == "Err" => Ok ( Value :: Bool ( false ) ) ,
2499+ other => Err ( RuntimeError :: type_error ( "Result" , other. type_name ( ) ) ) ,
2500+ }
2501+ }
2502+
2503+ /// Check if a Result value is Err.
2504+ /// Usage: is_err(result_value)
2505+ fn builtin_is_err ( args : & [ Value ] ) -> RuntimeResult < Value > {
2506+ if args. len ( ) != 1 {
2507+ return Err ( RuntimeError :: custom ( "is_err requires exactly 1 argument" ) ) ;
2508+ }
2509+
2510+ match & args[ 0 ] {
2511+ Value :: Struct { name, .. } if name. as_str ( ) == "Err" => Ok ( Value :: Bool ( true ) ) ,
2512+ Value :: Struct { name, .. } if name. as_str ( ) == "Ok" => Ok ( Value :: Bool ( false ) ) ,
2513+ other => Err ( RuntimeError :: type_error ( "Result" , other. type_name ( ) ) ) ,
2514+ }
2515+ }
2516+
24282517// ============================================================================
24292518// Tests for collection builtins
24302519// ============================================================================
0 commit comments