Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Scanf and objects
@ 2003-05-21 16:48 Christophe TROESTLER
  2003-05-21 18:42 ` Damien
  0 siblings, 1 reply; 2+ messages in thread
From: Christophe TROESTLER @ 2003-05-21 16:48 UTC (permalink / raw)
  To: O'Caml Mailing List

Hi,

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;;  
  Hello
  Exception:
  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

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

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

-------------------
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] 2+ messages in thread

end of thread, other threads:[~2003-05-21 18:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-05-21 16:48 [Caml-list] Scanf and objects Christophe TROESTLER
2003-05-21 18:42 ` Damien

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox