Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Scanf and objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-05-21 (16:46)
From: Christophe TROESTLER <debian00@t...>
Subject: [Caml-list] Scanf and objects

I am trying to understand how Scanf works and have some questions.
The comment preceding the definition of [kscanf ib ef fmt f] says

   If the entire scanning succeeds [...] the tokens are applied to [f].

But are they not applied before?  Consider

        Objective Caml version 3.06+33 (2003-05-19)

  let f i = print_string "Hello"; print_newline(); fun j k  -> i+j+k in
  let buf = Scanf.Scanning.from_string "3 4 x" in
    Scanf.bscanf buf "%i %i %i" f;;  
  Scanf.Scan_failure "scanf: bad input at char number 5: int_of_string".

The scanning has not succeeded, however the side effect took place.
What am I misunderstanding here?  As far as I get it, the first argument
is applied when the scanning of the second succeeds since it gives
[stack (stack f i) j] and the argument [stack f i] will be computed.
If arguments are applied to [f] before the scanning is complete, then
what is the purpose of [stack], why not apply them directly instead of
waiting for the next token ?

Now, is it possible to build objects through a format process?  More
precisely, I'd like to have a function that takes (something like) a
format string, say "%i %f", and creates an object like

    method fold : ('a -> int -> float -> 'a) -> 'a -> 'a

It does not look so easy to me since on one hand you would have to
parametrize the object to be able to build its type incrementally
(e.g. with a (int -> float -> 'a, 'a) type) but that would then forbid
the fold method to be fully polymorphic (not really nice...).  Is this
correct?  Are there workarounds?

Thanks for comments,

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: