klimpergeist
klimpergeist

Reputation: 363

OCaml function name as string

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?

Upvotes: 6

Views: 1568

Answers (3)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

There is a set of values available for debugging and error reporting.

OCaml 4.12 introduced __FUNCTION__:

val __FUNCTION__ : string

__FUNCTION__ returns the name of the current function or method, including any enclosing modules or classes.

They might be useful if you don't want to use assert false as suggested by @SteveVinoski.

__LOC__ : string
__FILE__ : string
__LINE__ : int
__MODULE__ : string
__POS__ : string * int * int * int

There are also fancier forms that you can use to wrap an expression to determine its extent in the source.

These are documented in the Stdlib module.

Upvotes: 14

glennsl
glennsl

Reputation: 29136

Since 4.12.0, __FUNCTION__ will return the function name.

There are also a number of other debugging variables defined in Stdlib:

val __LOC__ : string
val __FILE__ : string
val __LINE__ : int
val __MODULE__ : string
val __POS__ : string * int * int * int
val __FUNCTION__ : string
val __LOC_OF__ : 'a -> string * 'a
val __LINE_OF__ : 'a -> int * 'a
val __POS_OF__ : 'a -> (string * int * int * int) * 'a

Upvotes: 1

Thomash
Thomash

Reputation: 6379

I think you should try as much as possible to use more specific types so that the compiler can prevent you from calling your functions with invalid input. If you really don't find a way to tell the type system what you want, try harder, and if you really can't, then use assert which as Steve told you will give you some precious debugging information (file and line number are much more useful than function name since chances are high your function doesn't have a name).

Upvotes: 1

Related Questions