Search code examples
ocamltypeerror

Consider print in with block as return value


I got a problem when I wanted to return boolean value but I always got error (code below)

Error: This expression has type unit but an expression was expected of type bool

method addVehicle licensePlate : bool = 
  let isSuccess = ref false in
  try
    (* get current time to make sure unique id *)
    id <- int_of_float (Unix.time ());

    (* ask to what kind of vehicle to input *)
    let kind = ref 0 in 
    let quit_loop = ref false in
    while not !quit_loop do
      print_string "What kind of vehicle: {0-Car, 1-Motobike, 2-Wheelchair, -1-Exit}? ";
      kind := read_int ();

      if !kind == 0 then 
        (vehicle <- new car licensePlate 60;
         quit_loop := true;)
      else  if !kind == 1 then
        (vehicle <- new car licensePlate 60;
         quit_loop := true;)
      else if !kind == 2 then
        (vehicle <- new wheelchair licensePlate 60;
         quit_loop := true;)
      else if !kind == -1 then
        quit_loop := true
      else 
        print_endline "Invalid vehicle!"
           
      (* match !kind with
         |  0 -> vehicle <- new car licensePlate 60;        quit_loop := true;
         |  1 -> vehicle <- new motorbike licensePlate 60;  quit_loop := true;
         |  2 -> vehicle <- new wheelchair licensePlate 60; quit_loop := true;
         | -1 -> quit_loop := true;
         |  _ -> print_endline "Invalid vehicle!"; *)
    done;

    (* ask start time *)
    if not (!kind == -1) then 
      let quit_loop = ref false in
      while not !quit_loop do
        print_string "Input start time (format yyyy-MM-dd hh:mm): ";

        let input = read_line () in 
        try
          let (year, month, day, hour, minute) = sscanf input "%d-%d-%d %d:%d" (fun x y z t w -> (x, y, z, t, w)) in
          startTime <- snd (mktime { 
            tm_sec = 0; tm_min = minute; tm_hour = hour; tm_mday = day; 
            tm_mon = month - 1; tm_year = year - 1900; tm_wday = 0; tm_yday = 0; tm_isdst = false;
          });

          quit_loop := true;
          isSuccess := true;
        with
        | _ -> print_string "Invalid start time!";
      done;
  with
  | _ -> print_string "Something went wrong when adding vehicle";

  match isSuccess with
  | _ -> !isSuccess;

I want to fix this problem to make my method return boolean value!


Solution

  • First, equality is = in OCaml, the result a == b is not specified if a and b are not mutable values. You wanted to write a=b.

    Second, you are misusing ;: ; is not a terminator in OCaml, it is a kind of binary operator on expressions.

    Third, your indentation is lying to you. Your code reads as

      with
      | _ -> 
        (print_string "Something went wrong when adding vehicle";
        match isSuccess with
        | _ -> !isSuccess)
    

    when you probably intended to write

    (try ...
     with
     | _ -> print_string "Something went wrong when adding vehicle"
    );
    match isSuccess with
    | _ -> !isSuccess
    

    Fourth, I will advise to split your methods in well-delimited functions: it would make it much easier to catch programming and syntax errors early. In particular, wide try ... with ... clause should be avoided as much as possible because it hides the origin of exceptions.