exception - OCaml : Raise an error inside a match with structure -
in ocaml, have list of strings contains names of towns (something "1-new york; 2-london; 3-paris"). need ask user type number (if want london have type 2). want raise exception message saying town not valid, if person types example "4", in example. tried this, doesn't work :
let chosentown = match int_of_string (input_line stdin) | x > (length listoftowns) -> raise (err "not valid town")
what's way code "if chosen number bigger length of list raise error" ??
pattern can't contain arbitrary expressions. can constant, constructor name, record field inside curly braces, list, array, etc.
but patterns can guarded, e.g.
match int_of_string (input_line stding) | x when x >= length listoftowns -> invalid_arg "the number large" | x -> list.nth listoftowns x
Comments
Post a Comment