# Exercise: Pattern Matching
#
# Use match/end to handle each variant case:
#
#   my-option match
#     Some { >value } -> use-value
#     None -> handle-empty
#   end
#
# When matching, { >field } extracts that field onto the stack.
#
# Example from stdlib:
#   : unwrap-or ( Option Int -- Int )
#     swap match                    # ( default Option )
#       Some { >value } -> nip      # ( default value ) -> ( value )
#       None ->                     # ( default ) -> just return it
#     end
#   ;
#
# Remember the (value Bool) pattern from chapter 12? You had to check
# the Bool or drop it. Here you'll implement `unwrap-or` for your
# custom Option type - providing a default value when None.
#
# Your task: Implement unwrap-or using match
#
# When done, delete the marker line below.

union Option { Some { value: Int } None }

# I AM NOT DONE

: unwrap-or ( Option Int -- Int )
    # If Some, return the value. If None, return the default.
    # Hint: bring the Option to the top of the stack so you can
    # match on it. In the Some branch you'll have both the default
    # and the extracted value present — keep only the value. In
    # the None branch, the default is already what you want.
    drop drop 0
;

: test-unwrap-some ( -- )
    42 Make-Some 0 unwrap-or
    # Do not edit below this line
    42 test.assert-eq
;

: test-unwrap-none ( -- )
    Make-None 99 unwrap-or
    # Do not edit below this line
    99 test.assert-eq
;
