I sometimes write functions that make the assumption that some arguments cannot occur. If they do, this is a bug and I fail:
let foo = function
| 0 -> ()
| _ -> failwith "foo: bad argument"
If I rename the function later on, I have to remember to also change the string. Is there a way to do this in a more systematic manner? My mind wanders around solutions like
| _ -> failwith (FUNCTION_NAME ^ ": bad argument")
where FUNCTION_NAME
is a string variable that the compiler or interpreter will instantiate. But I have no idea whether something like this even works in OCaml. If not, is there a best practice?
assert false
or similar the resulting error message will contain file and line number information. Perhaps that's adequate for this case? – Blok1+1
there are two functions one of which doesn't have a name:(+)
is the function that has a name and(+) 1
is the one that doesn't. For this reason line numbers are a lot better. – Piercy