0

In a previous question asked I learned that a separate file is forcibly a module. This is of course true for a class definition in a file. I have a file point.ml with the class point, compiled, and are doing in the toplevel:

# #load "point.cmo";;
# let p = new point (1,1);;
Error: Unbound class point
# let p = new Point.point (1,1);;
val p : Point.point = <obj>
# 

Is there a way to get rid of the outer module Point?

Accepted solution

I asked two times, and there seems to be no way around automatic module generation per file. So one has to adjust to it, and either open the module, or make best use of module and object name to turn this into a feature. There still are module aliases, and the module name of an object is quite irrelevant after instantiation.

Community
  • 1
  • 1
Str.
  • 1,389
  • 9
  • 14

1 Answers1

1

You can open the module:

# open Point;;

It doesn't get rid of it, but it opens up the top-level names for use without qualification by the module name.

For quick testing you can also do #use to read a source file without creating a module:

# use "point.ml";;

The top-level acts like you have typed in the whole file.

If you have source files named apple.ml and orange.ml that define single classes, you can just name the class t in each file. From other modules you can then refer to the classes as Apple.t and Orange.t. This has worked out well for me. If you do this, you definitely don't want to open the modules.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108