caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Baptiste Rouquier <jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Managing a polymorphic environment
Date: Thu, 12 Aug 2004 17:06:38 +0200 (CEST)	[thread overview]
Message-ID: <1092323198.411b877e22214@mouette.ens-lyon.fr> (raw)
In-Reply-To: <Pine.A41.4.44.0408121629060.557174-100000@ibm1>

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


  reply	other threads:[~2004-08-12 15:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-12 14:46 Diego Olivier Fernandez Pons
2004-08-12 15:06 ` Jean-Baptiste Rouquier [this message]
2004-08-12 15:19   ` Diego Olivier Fernandez Pons
2004-08-12 17:05 ` brogoff
2004-08-13  8:06   ` Diego Olivier Fernandez Pons
2004-08-13 10:12     ` Diego Olivier Fernandez Pons
2004-08-13 14:20 Jean-Baptiste Rouquier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1092323198.411b877e22214@mouette.ens-lyon.fr \
    --to=jrouquiethearchiveshouldhaveafewantispamtricks@ens-lyon.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).