caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] better "encapsulation" for type Digest.t ?
@ 2017-02-08 13:04 Mohamed Iguernlala
  0 siblings, 0 replies; only message in thread
From: Mohamed Iguernlala @ 2017-02-08 13:04 UTC (permalink / raw)
  To: caml-list

Hi,

I am wondering why the type t of Digest module is not abstract or
defined as private. Currently, this type is an alias of string. Thus,
one can easily make a mistake by providing a string instead of a
Digest.t to function Digest.to_hex, which raises an exception (ex1
below). The type-checker would help to catch this kind of errors if
Digest.t was abstract or private.

(*
   from Digest.mli:
     type t = string
     val to_hex : t -> string
     val string : string -> t
*)

let ex1 =
   try ignore (Digest.to_hex "foobar")
   with Invalid_argument("index out of bounds") -> Format.eprintf "Aie !@."

let ex2 =
   ignore (Digest.to_hex (Digest.string "foobar"));

- Mohamed.

-- 
Senior R&D Engineer, OCamlPro SAS
Research Associate, VALS team, LRI
Webpage: http://www.iguer.info
LinkedIn: https://fr.linkedin.com/in/mohamed-iguernlala-71515979

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-02-08 13:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-08 13:04 [Caml-list] better "encapsulation" for type Digest.t ? Mohamed Iguernlala

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).