Re: type and type string option

"Philippe Veber [email protected] [ocaml_beginners]" <[email protected]> Wed, 18 May 2016 18:34:54 +0200
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAOOOohTOBhZNbk0MCd0oces_M+CRLOdoOuRkP3Aq-6vO7vRZGg@mail.gmail.com>
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.



2016-05-18 16:41 GMT+02:00 cedlemo [email protected] [ocaml_beginners] <
[email protected]>:

> I have this little Ocaml code that should print the value of an
> environment variable specified as an anonymous argument. If no argument
>
> is given, the environment variable content to print is EDITOR.
>
>
> open Core.Std
>
> let spec =
>
> let open Command.Spec in
>
>     empty
>
>     +> anon (maybe ("var_name" %: string))
>
> let print_value variable ()=
>
>    let getenv_value_of varname =
>
>      try
>
>        Sys.getenv varname
>
>      with Not_found -> varname ^ " not defined"
>
>    in match variable with
>
>    | None -> print_endline (getenv_value_of "EDITOR")
>
>    | Some name -> print_endline (getenv_value_of name)
>
>
> let command = Command.basic
>
>     ~summary:"Print the value of an environment variable"
>
>     ~readme:(fun () -> "Print the value of an environment variable you
> provide or the EDITOR variable it you didn't provide any")
>
>     spec
>
>     print_value
>
>
> let () = Command.run ~version:"1.0" ~build_info:"RWO" command
>
>
> If I try to compile it with corebuild -quiet print_en.native
>
>
> I have this error:
>
>
> + ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g
> -bin-annot -short-paths -thread -package core -ppx 'ppx-jane -as-ppx' -o
> MrHide_env_variables.cmo MrHide_env_variables.ml
> File "MrHide_env_variables.ml", line 13, characters 19-43:
> Error: This expression has type string but an expression was expected of
> type
>           string option
> Command exited with code 2.
> Hint: Recursive traversal of subdirectories was not enabled for this build,
>    as the working directory does not look like an ocamlbuild project (no
>    '_tags' or 'myocamlbuild.ml' file). If you have modules in
> subdirectories,
>    you should add the option "-r" or create an empty '_tags' file.
>
>    To enable recursive traversal for some subdirectories only, you can
> use the
>    following '_tags' file:
>
>        true: -traverse
>        <dir1> or <dir2>: traverse
>
> I can not see how to solve this.
>
>
> cedlemo
>
>
>
> ------------------------------------
> Posted by: cedlemo <[email protected]>
> ------------------------------------
>
> Archives up to December 31, 2011 are also downloadable at
> http://www.connettivo.net/cntprojects/ocaml_beginners
> The archives of the very official ocaml list (the seniors' one) can be
> found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> ------------------------------------
>
> Yahoo Groups Links
>
>
>
>