Browse thread
[Caml-list] Infix operators
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | skaller <skaller@u...> |
| Subject: | Re: [Caml-list] Infix operators |
On Sat, 2004-08-14 at 22:59, Erik de Castro Lopo wrote: > Hi all, > > I've got a number of defined types for which I'd like to define > infix operators for addition, subtraction, multiplication etc. > > Unfortunately since each type needs is own infix operator I'm > running out of ideas unique operator versions for each type. > > Why is it not possible to do something like: > > let ( ++ ) (a:float) (b:float) = a +. b ;; > > let ( ++ ) (a:int) (b:int) = a + b ;; There reason, roughly, is that it is very hard to combine type inference with overloading. It can be done (there's a paper on Citeseer describing an algorithm). It isn't clear what the error message quality would be though. The GCaml solution Richard Jones mentions: # generic plus = | int -> int -> int => (+) | float -> float -> float => (+.) ;; When you use this: plus 1 1 plus 1.0 1.0 there is only a *single* plus function, with several explicitly given typings -- thus avoiding the problem of combining inference with overloading neatly. -- John Skaller, mailto:skaller@users.sf.net voice: 061-2-9660-0850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners