# Exercise: Result Type
#
# The Result type represents success or failure.
#
# union IntResult {
#   Ok { value: Int }
#   Err { error: String }
# }
#
# The compiler generates:
#   Make-Ok   ( Int -- IntResult )
#   Make-Err  ( String -- IntResult )
#
# Examples:
#   42 Make-Ok           # Success with value 42
#   "failed" Make-Err    # Failure with message
#
# Use match to check which variant:
#   my-result match
#     Ok { >value } -> value           # extract value on success
#     Err { >error } -> drop 0         # handle error (drop message)
#   end
#
# NOTE: We define our own IntResult here to learn how variants work.
# Seq's stdlib uses the simpler (value Bool) pattern for error handling -
# remember `string->int` from chapter 12? It returns (Int Bool).
# This exercise shows how you COULD implement a Result type as a union,
# which is useful for understanding ADTs and when you need error messages.
#
# Seq 4.0 auto-generates: is-Ok?, is-Err?, Ok-value, Err-error
# We implement our own to understand the underlying match pattern.
#
# Your task: Implement is-ok? and is-err? using match, then create
# success and failure wrapper functions.
#
# When done, delete the marker line below.

union IntResult {
    Ok { value: Int }
    Err { error: String }
}

# I AM NOT DONE

: is-ok? ( IntResult -- Bool )
    # Return true if Ok, false if Err
    # Your code here - use match
    drop false
;

: is-err? ( IntResult -- Bool )
    # Return true if Err, false if Ok
    # Your code here - use match
    drop false
;

: success ( Int -- IntResult )
    # Wrap an integer in Ok
    # Your code here - use Make-Ok
    drop "error" Make-Err
;

: failure ( String -- IntResult )
    # Wrap an error message in Err
    # Your code here - use Make-Err
    drop 0 Make-Ok
;

: test-ok ( -- )
    42 success is-ok?
    # Do not edit below this line
    test.assert
;

: test-err ( -- )
    "oops" failure is-err?
    # Do not edit below this line
    test.assert
;
