'This expression has type processor_return_type but an expression was expected of type unit

I now have a function called processor, inside the processor function, a list will be matched to different patterns. In some patterns I wish it to return a tuple while the rest calls processor again.

Suppose I now have a custom type to wrap two types of processor:

type processor_return_type = 
| REC of unit
| INFO of (string list * bool)

My processor basically looks like this:

let rec processor cmds stack env = 
  match (cmds, stack) with
  | (ADD::rest_cmds, first_list::rest_stack) -> ... processor a b c
  ...
  | (FUN::...) -> ... let (sl, b) = processor a b c in processor d e f
  | (RETURN::...) -> (string list, a bool)
  | _ -> REC()
  ...
in

Then I invoke this function with (you can assume I give correct arguments):

processor cmd_list [[]] [[]];;

The error emerges:

    664 |   processor cmd_list [[]] [[]];;
    
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type processor_return_type
       but an expression was expected of type unit

How could I fix this issue?



Solution 1:[1]

Thanks for including the error message and the indicated erroneous part of the code.

The expression that the compiler is complaining about is the entire call to processor. In other words, the reason the compiler is expecting something of type unit is because of the context of the call, which you unfortunately don't show.

Generally your code should expect to get a result back from the call to processor and should handle the result appropriately. If there are other branches of your code (the else part of an if, say), they have to be of the same type also.

Here's an example of an erroneous call context:

if some_test () then
    Printf.printf "did not want to call processor\n"
else
    proceessor ...

If you show more of the context of the call, people can give you a more helpful answer maybe.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1