Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Understanding why Ocaml doesn't support operator overloading.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michal Moskal <malekith@p...>
Subject: Re: [Caml-list] Understanding why Ocaml doesn't support operator overloading.
On Fri, Nov 29, 2002 at 04:52:03PM -0000, Nicolas Cannasse wrote:
> Of course the ML type system relies on type inference and need do choose the
> "best available" type but what if we enrich the type system with an "OR"
> operator ? Then if (+) is overloaded on floats, you'll get :
> 
> f :  int -> int OR float -> float
> or something like a type constraint :   f : 'a -> 'a where 'a in [int;float]
> 
> This approach seems trivial to me, but I really can understand that this
> require a lot of addins in the typing algorithms & theory. BTW, does one of
> the upper approach has already been discussed ? any paper on it ? any
> countersample that will make me feel stupid ? :)

The problem is what *assembly code* should be generated for function f?
Code to add 2 integers or code to add 2 floats? Hmm.. we'll have a
problem then. Or maybe both? And choose versions of f based on type it
is applied to? But then consider:

let f x1 x2 ... xn = ((x1 + x1), (x2 + x2), ..., (xn + xn))

you need to generate 2^n versions of f. We're getting to ugly things
like C++ templates here.

There is third answer: generate code that checks if it's passed int or
float. But this has very significant perfomance impact.

-- 
: Michal Moskal ::::: malekith/at/pld-linux.org :  GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::: Wroclaw University, CS Dept :  {E-,w}-- {b++,e}>+++ h
-------------------
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