On Wed, Apr 11, 2012 at 11:32 AM, David House <dho...@janestreet.com> wrote:
> On Wed, Apr 11, 2012 at 11:26 AM, Goswin von Brederlow
> <goswin-...@web.de> wrote:
>>> However in the particular case of finding something in a data structure 
>>> where the user could be confronted with a situation where he can prove that 
>>> the datum will be found I think it's justified to provide both flavours. 
>>> For these cases, in my own code I settled on the following pattern :
>>>
>>>     val find : 'a t -> key -> 'a option
>>>     (** [find d k] is the value bound to [k] in [d], if any. *)
>>>
>>>     val get : 'a t -> key -> 'a
>>>     (** [get d k] is like {!find} but raises [Invalid_argument] if [k] is 
>>> not bound in [d]. *)
>>
>> That pattern works well if you have just one or few such functions and
>> can think of nice names to differentiate them.
>
> A common pattern that is convenient and clear is to append _exn to the
> functions that raise excetions on failure.

As an example, see Core's Map module:

https://bitbucket.org/yminsky/ocaml-core/src/c0e9df7b574d/base/core/lib/core_map_intf.ml

We have, e.g., [find] and [find_exn].


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to