Search code examples
objectmethodscallocaml

Multiple methods call in a method (OCaml)


I would like to call more than 3 methods in the same method. Let's say that I want to set three different variables of an object with three different "set" method.

What is the best way to do it?

I tried with "and" but it doesn't seem to work.


Solution

  • In OCaml, ";" is the sequence operator. Expression "a; b" evaluates expression a, discards its result (which should be unit), evaluates b, then returns the result of b.

    In OCaml, a function call or a method call is nothing but an expression.

    So, you can write something like this:

    method my_method =
      my_object#set_x 10;
      my_object#set_y 50;
      my_object#set_z 30