Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Managing a polymorphic environment
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Baptiste Rouquier <jrouquiethearchiveshouldhaveafewantispamtricks@e...>
Subject: Re: [Caml-list] Managing a polymorphic environment
Quoting Diego Olivier Fernandez Pons <Diego.FERNANDEZ_PONS@etu.upmc.fr>:

> let x = ...
> Env.add x "the number of elements in the knapsack"
> let c = ...
> Env.add c "the total cost of the knapsack"
> 
> 
> The idea is of course to be able to pretty-print the result
> 
> # Env.print_int x;;
> the number of elements in the knapsack is 10 - : unit = ()
> # Env.print_float c
> the total cost of the knapsack is 100.0 - : unit = ()

I'm finishing a lib to handle configuration files, which meets the same kind of
problem : one would like pretty print (and read back) the values, but handle
them in type safe way inside the programme.
I'm using objects and I think it can solve your problem : have a class Env.foo
that has a virtual method print (and any other method to implement the functions
you want in Env), and have classes foo_int and foo_float that inherit from it.
Then your code will be written

let x = new foo_int 10 "the number of elements in the knapsack"
Env.add x (*x is coerced to foo, we forget it's an int*)
let c = new foo_float "the total cost of the knapsack"
Env.add c 

x#print
c#print


I can send you my code if you want more details.

Jean-Baptiste.
http://perso.ens-lyon.fr/jean-baptiste.rouquier

-------------------
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