Re: type and type string option
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
On 18/05/2016 18:34, Philippe Veber [email protected] [ocaml_beginners] wrote: > Hi, > > the problem lies in the expression: > ``` > try > Sys.getenv varname > with Not_found -> varname ^ " not defined" > ``` > The body of a try ... with expression (here it is `Sys.getenv > varname`) and the expression for each case of the `with` part should > have the same type. Since you're using core, `Sys.getenv` returns a > string option, but `varname ^ " not defined"` is of type string, hence > the type error. > > Hope this helps, > Philippe. Hi Philippe, I have changed the try ... with to a match .. with match Sys.getenv varname with | None -> "Empty" | Some n -> n and I can compile it but some points remain unclear for me. First I have read the documentation of Sys: val getenv : string -> string Return the value associated to a variable in the process environment. Raise Not_found if the variable is unbound. Then I have read too that when you want to deal with raised exception, you use try ... with. So OK it doesn't work, I must use macth ... with. So in this case what is the point to raise an exception Not_found ? Another point that I don't understand is : > Since you're using core, `Sys.getenv` returns a string option Does that mean that like in the documentation Sys.getenv returns a string but when used used with the Core module it returns a string option ? Thanks for your help. cedlemo