caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Operators for Int64 and Int32
@ 2008-04-03 14:08 Michał Maciejewski
  2008-04-03 15:24 ` David Allsopp
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Michał Maciejewski @ 2008-04-03 14:08 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2008-04-06 17:34 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-03 14:08 [Caml-list] Operators for Int64 and Int32 Michał Maciejewski
2008-04-03 15:24 ` David Allsopp
2008-04-03 15:44   ` Bünzli Daniel
2008-04-03 19:00   ` David Thomas
2008-04-03 19:50   ` Michał Maciejewski
2008-04-03 22:22     ` Jon Harrop
2008-04-03 17:15 ` Richard Jones
2008-04-03 20:17 ` Erik de Castro Lopo
2008-04-03 20:39   ` Michał Maciejewski
2008-04-03 22:02     ` Erik de Castro Lopo
2008-04-03 22:17   ` Richard Jones
2008-04-04  1:47     ` Erik de Castro Lopo
2008-04-04 17:58       ` Adrien
2008-04-04 21:07         ` Richard Jones
2008-04-06 17:34           ` Adrien

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).