0

The package Batteries.Num overrides the functions (+), (-), ... So the compiler gives an error on the following simple code

open Batteries
open Num
let a = 4 + 4;;

File "a.ml", line 3, characters 8-9: Error: This expression has type int but an expression was expected of type Batteries.Num.num = Num.num

I compile with "ocamlfind ocamlc -package batteries a.ml".

EDIT: I know I could use Pervasives.(+) or just open Num locally.

But I am able to compile this program successfully with ocamlbuild with the simplest _tags file: <*>: pkg_batteries, package(batteries), package(num)

Where is the magic? How can I compile like ocamlbuild does with a Makefile?

EDIT: I found the solution. I actually had two versions of batteries (1.4 and 2.2), and ocamlfind gave the version 2.2 on command line, and 1.4 on ocamlbuild. I don't know why. I believe the early version of batteries didn't redefine the module Num (but you had to use BatNum), so the problem doesn't occur with the older version. Thanks for the answers.

cankosa
  • 43
  • 3

3 Answers3

0

What command are you using the build it? Because if you aren't specifying a build target (i.e. your command is ocamlbuild a.ml instead of ocamlbuild a.ml a.native) it will appear as though its compiling even though it isn't.

What is ACTUALLY happening when you try "ocamlfind ocamlc -package batteries a.ml" is that the + from nums is overshadowing the + from Pervasives.

You could do let a = Pervasives.(+) 4 4;;, or instead of opening Nums, do Nums. for everything like that.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
sacooper93
  • 86
  • 2
  • Yes, I know I could use Pervasives.(+), or just use let open Num in whenever necessary. My problem is that I have a code that compiles fine with ocamlbuild, but not on command line (or a Makefile). I use "ocamlbuild -use-ocamlfind a.byte" which does produce an executable, and again "ocamlfind ocamlc -package batteries a.ml" gives a type error. Basically I have a larger project that compiles fine with ocamlbuild, but I need to switch to OCamlMakefile which fails for this reason. – cankosa Jun 29 '14 at 09:16
0

You can just refer to the function by using the module name you want.

If you want the (+) from Pervasives, you just say Pervasives.(+) x y instead of x + y.

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
0

The solution I have used when I faced this problem is to open Pervasive to override Batteries values.

open Batteries
open Num
open Pervasives

let a = 4 + 4;;
Thomash
  • 6,339
  • 1
  • 30
  • 50