OCaml classes: required to define all methods on the spot?

Context: I’m reading Classes - Real World OCaml

Question: In Rust, we an do the following:

pub struct Foo { ... }; // defines class Foo

pub trait Some_Trait {
  ... list a bunch of function signtures ...
}

impl Some_Trait for Foo {
  ... implement the methods here ...
}

So note, we can separate two things: (1) defining the fields of struct Foo, and (2) implementing the methods of struct Foo

===

In the OCaml docs I have seen above, it seems all the methods are defined inline where we define the class. Thus, my question:

Are we able to separate the two, i.e.:

(1) define the fields of an OCaml class in one ml file
(2) add some methods to it in another ml file ?

Or, are we required to define all methods of the class at the moment we list the fields of the class ?

Thanks!

Yes:

class type t =
  object
    method foo: int
  end

class c : t =
  object
    method foo = 42
  end

I recommend the manual to learn more: OCaml - Objects in OCaml

Cheers,
Nicolas

1 Like

On the tutorial side, with a quick internet search I found this: Mixin objects by Alain Frisch | LexiFi.

P.S. My slides on the expression problem.