English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] format4 query
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-05-19 (06:17)
From: David J. Trombley <swampy@s...>
Subject: [Caml-list] format4 query

Suppose I have

type listener = string -> unit

which is intended to receive a string type as a value.

Could someone demonstrate a function that takes a listener
and a format as arguments, and uses sprintf to obtain a
string, and then calls the listener with the string
argument? No matter how I seem to do this, the first
parameter of the format4 type seems to get inferred to
string, causing the parser to not consume all of the
intended format tokens, causing a 'too many arguments'
error.  For example:

# type listener = string -> unit;;
type listener = string -> unit
# let r s = printf "%s\n" s;;
val r : string -> unit = <fun>
# let q (a : listener) b = let str  = sprintf b in a str;;
val q : listener -> (string, unit, string) format -> unit =
(* already messed up as evidenced by the first 'string' *)
# q r "%s ya" "hey";;
This function is applied to too many arguments, maybe you
forgot a `;'

I've discovered the kprintf function, which looked promising
since I could get simple examples to work, but utterly fails
in the same stupid way when I put it in a context that is
not a toplevel evaluation. (ie. I try to use this in some
functional way, and the type inference screws up).

Isn't there some way to simply define a function that takes
a format4 arg in some opaque way so that the type of the
result of the kprintf doesn't mess up the type of the
original function by some complicated inference mechanism?

My actual code currently looks something like this:

   Hashtbl.iter ( function cbx ->
        x when x.minLevel <= level ->
           (try (Hashtbl.find x.catchExnTable error) error
            with Not_found -> let ccb = x.callback stream
level error in
                                        Printf.kprintf ccb
      | x -> ()
     ) stream.listeners;

and the types are

val stream : log_stream
val level : log_level
val error : log_error
val fmt : <some sort of format4 that is broken>

type log_callback = (log_stream -> log_level -> log_error ->
string -> unit)
 and log_stream   = { streamName : string;
                      listeners : (log_callback, log_entry)
Hashtbl.t }
 and log_entry    = { callback : log_callback;
                      minLevel : log_level;
                      expectList : (int * log_error) list;
                      catchExnTable : (log_error,
log_error -> unit) Hashtbl.t }

    David J. Trombley

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