caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] format4 query
@ 2004-05-19  6:15 David J. Trombley
  2004-05-19  7:22 ` Olivier Andrieu
  0 siblings, 1 reply; 4+ messages in thread
From: David J. Trombley @ 2004-05-19  6:15 UTC (permalink / raw)
  To: caml-list; +Cc: hmaon


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 =
<fun>
(* 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 ->
      function
        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
fmt)
      | 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 }

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-05-19 17:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-19  6:15 [Caml-list] format4 query David J. Trombley
2004-05-19  7:22 ` Olivier Andrieu
2004-05-19 16:13   ` David J. Trombley
2004-05-19 17:20     ` Damien Doligez

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