caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ingo Bormuth <ibormuth@efil.de>
To: "Jacques Garrigue" <garrigue@math.nagoya-u.ac.jp>,
	"Stéphane Glondu" <Stephane.Glondu@crans.org>
Cc: caml-list@inria.fr
Cc: ingo@bormuth.org
Subject: [SOLVED] Reference to polymorphic function ?
Date: Thu, 15 Sep 2005 12:31:48 +0200	[thread overview]
Message-ID: <20050915103148.GA21415@kruemel> (raw)
In-Reply-To: <20050915.084814.127622622.garrigue@math.nagoya-u.ac.jp>

[-- Attachment #1: Type: text/plain, Size: 2298 bytes --]


On 2005-09-15 08:48, Jacques Garrigue wrote:
>
> The simplest way to do this is to define a new type:
> 
> type put = {put: 'a -> unit} ;;
> let put = {put = put_to_screen} ;;
> put.put "test";;
> put.put 5;;
> 
Thank you !!! That's exactly what I was looking for.


On 2005-09-14 23:08, Stéphane Glondu wrote:
>
>                                        However, I didn't catch what 
> you want to do with thoses references: maybe there is another way to 
> achieve what you want?
> 

Just for the notes: 

I wrote a serilisation function to pack a quite complex data structure 
into one string. That string is then used to feed a file, socket, 
sha1 or encryption algorithm. 
I want to reduce overhead and directly marshal the values to the 
destination without generating the intermediate string whenever possible.

This is a simple profe of principal prototype :


type data =
  | Int of int
  | Float of float
  | Str of string
;;

type target = 
  | Screen
  | Channel of out_channel
;;

type put =
  {
    str: string -> unit ;
    raw: 'a. 'a -> unit
  }
;;

let serialize data target =
  let put = ref {
        str = ( fun s -> () );
        raw = ( fun v -> () ) } in
  let write_value key value =
    !put.str ( "<" ^ key ^ ">" ) ;
    !put.raw value ;
    !put.str ( "</" ^ key ^ ">" ) in
  let write_data () =
    match data with
     | Int i -> write_value "int" i
     | Float f -> write_value "float" f
     | Str s -> write_value "str" s in
  let init_target =
    match target with
      | Screen -> put := { 
            str = print_string ;
            raw = let f v = print_string ( Marshal.to_string v [] ) in f }
      | Channel chan -> put := { 
            str = output_string chan ;
            raw = let f v = ( Marshal.to_channel chan v [] ) in f } in
  init_target ;
  write_data ()
;;


serialize ( Int 123 ) Screen 
;;

serialize ( Float 3.14 ) Screen 
;;

serialize ( Str "Hello" ) ( Channel ( open_out "/tmp/seri" ) ) 
;;

-- 
   +--------------------------------------------------------+
   | Ingo Bormuth,  voicebox & telefax: +49-12125-10226517  |
   | GnuPG key 86326EC9 at http://ibormuth.efil.de/contact  |
   +--------------------------------------------------------+

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

      parent reply	other threads:[~2005-09-15 10:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-13 12:38 Ingo Bormuth
2005-09-14 21:08 ` [Caml-list] " Stéphane Glondu
2005-09-14 23:48 ` Jacques Garrigue
2005-09-15  6:31   ` Olivier Andrieu
2005-09-15  7:00     ` Jacques Garrigue
2005-09-15 10:31   ` Ingo Bormuth [this message]

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=20050915103148.GA21415@kruemel \
    --to=ibormuth@efil.de \
    --cc=Stephane.Glondu@crans.org \
    --cc=caml-list@inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    /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).