views:

211

answers:

2

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?

+1  A: 

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
nlucaroni
You're exactly right. I got into the habit of using capitalization for class names per some other languages and keep reverting to that with OCaml. Thanks!
Mat
eah. just to be clear. you can take that intuition when referring to modules and other files (since they are implicit modules)
nlucaroni
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?
Mat
i will edit in a moment...
nlucaroni
+1  A: 

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;;`
Rémi
wasn't sure if that worked with the class keyword, and didn't have time to check right now. correct.
nlucaroni