Mat Kelly
Mat Kelly

Reputation: 2366

Creating objects on-the-fly in OCaml

I am attempting to learn OCaml by using compiled code instead of the top-level; however, much of the sample code online seems to appeal to the latter.

I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition.

class bar =
object (self)
 method doFooProc = (new Foo "test")#process
end;;

class foo (param1:string)=
object (self)
 method process = Printf.printf "%s\n" "Processing!"
 initializer Printf.printf "Initializing with param = %s\n" param1
end;;

Additionally, the "let" syntax don't seem to be friendly within class definitions. Why is that?

class bar =
object (self)
 method doFooProc = 
  let xxx = (new Foo "test");
  xxx#process
end;;

class foo (param1:string)=
object (self)
 method process = Printf.printf "%s\n" "Processing!"
 initializer Printf.printf "Initializing with param = %s\n" param1
end;;

How do I go about creating a new object of class foo in the doFooProc method and call the instantiated foo's process command?

Upvotes: 4

Views: 726

Answers (2)

nlucaroni
nlucaroni

Reputation: 47934

You're mostly correct, but are either confusing syntax with the module system, or thinking of other languages. Take my considerations and you should be good!

I would like to create a new Foo within a method of an object per below. This code does not compile, citing a syntax error with the doFooProc definition.

Lowercase "foo" for objects, modules are uppercase. Also, you must put the definition of foo above the object that calls it. You should get an Unbound class foo if this happens.

class bar =
object (self)
 method doFooProc = (new foo "test")#process
end;;

Additionally, the "let" syntax don't seem to be friendly within class definitions. Why is that?

Because you don't have a matching in, instead you have a semi-colon. Then it will work. Also, you can remove those extra parens, but it doesn't matter.

class bar =
object (self)
 method doFooProc = 
  let xxx = (new Foo "test") in
  xxx#process
end;;

If, say, a method in foo instantiated a bar as well, is there any way to escape the problem that arises with ordering the class definitions within the source file?

Yes. It's just like writing mutually recursive functions and modules, you connect them with the and keyword.

class bar =
  object (self)
    method doFooProc = (new foo "test")#process
  end

and foo (param1:string) = 
  object (self)
    method process = Printf.printf "%s\n" "Processing!"
    initializer Printf.printf "Initializing with param = %s\n" param1
  end

Upvotes: 2

Rémi
Rémi

Reputation: 8332

For two mutually recursive class, use the and keyword

class bar =
  object (self)
    method doFooProc = 
      let xxx = (new foo "test") in
      xxx#process
  end
and foo (param1:string)=
  object (self)
    method process = Printf.printf "%s\n" "Processing!"
    initializer Printf.printf "Initializing with param = %s\n" param1
    method bar = new bar
  end;;`

Upvotes: 2

Related Questions