Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml syntax error with let ... in

I have a quite simple issue I think, but no way to figure out what's going wrong. I want to open a file and try to failwith a custom message if the file don't exist or something else.

Here my code (sorry for french comment):

if (argc = 1) then
    aide ()
else
    (* Si plus d'un argument, on récupère le type *)
    if argc >= 2 then
        let stage = int_of_string (Sys.argv.(1)) in
            if stage != 0 && stage != 1 then
                aide ()
            else
                ()
    else
        ()
    ;    
    (* Si plus de deux arguments, on récupère aussi l'entrée *)
    if argc >= 3 then
        let filename = Sys.argv.(2) in
        let input =
        try
            open_in filename
        with _ -> failwith ("Impossible d'ouvrir le fichier " ^ filename)
    else
        ()
    ;
;;

I've a syntax error on the with keyword. Someont have an idea ? Thanks.

like image 310
Atikae Avatar asked Feb 06 '26 13:02

Atikae


1 Answers

The error occurred because you bound input to a value but did not return anything in the then branch.

You should do something with the value input and return () after the try/with block.

if argc >= 3 then
    let filename = Sys.argv.(2) in
    let input = (* The error is in this line *)
    try
        open_in filename
    with _ -> failwith ("Impossible d'ouvrir le fichier " ^ filename)
else
    ()
like image 103
pad Avatar answered Feb 12 '26 09:02

pad