Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Operators for Int64 and Int32
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Micha³_Maciejewski <michal.m.pl@g...>
Subject: [Caml-list] Operators for Int64 and Int32
Hello,

I'm not sure whether i should post this message here or in beginners
area (which is probably dead for a long time).

I'm quite new to OCaml but recently I had to write a wave file parsing
application. I've decided to try OCaml instead of C++. The first
problem I've encountered was of course too short native Integer type
for representing some values in wave file header. So I was forced to
use int64 type instead.

Why not to add real operators in another module for non-native types
like int64 or int32 to standard library? It's not too elegant to
write:

Int64.add (Int64.mul (Int64.add a b) b) c;;

especially when you try to avoid exceeding a limit of 79 characters in
line. Putting a part of expression in the next line isn't also a good
idea due to readability and conciseness of code. For me writing some
expressions in that way took even 5 too 7 lines. Opening Int64 could
help, but then it would be possible to write:

of_int someint;;

It's not clear what type someint is casted to.

Besides modifying application is very difficult now. Imagine that we
want to change type of a, b and c to int. We have to change previous
expression to (a+b) * b + c it can be very hard for more complex
expressions.

The best solutions to those problem would be in my opinion to add
something like this to standard library (to new module):

let ( +^^ ) a b = Int64.add a b
let ( -^^) a b = Int64.sub a b
let ( *^^ ) a b = Int64.mul a b
let ( /^^ ) a b = Int64.div a b
let ( !^^ ) a = Int64.neg a
let ( %^^ ) a b = Int64.sub a (Int64.mul (Int64.div a b) b)

let ( +^ ) a b = Int32.add a b
let ( -^ ) a b = Int32.sub a b
let ( *^ ) a b = Int32.mul a b
let ( /^ ) a b = Int32.div a b
let ( !^ ) a = Int32.neg a
let ( %^ ) a b = Int32.sub a (Int32.mul (Int32.div a b) b)

the same for big_int

Note that It also preserves backward compatibility of OCaml.

Best regards
Michal Maciejewski