Loading ...
Sorry, an error occurred while loading the content.

RE: "ocaml_beginners"::[] Re: Instanciating mutually recursive classes.

Expand Messages
  • Stefan Lampe
    Hi, I missed the original email, but I often initialise mutually recursive objects. However, to achieve it I use lazy values. Not as pretty as using records,
    Message 1 of 5 , Jan 10, 2011
    • 0 Attachment
      Hi,

      I missed the original email, but I often initialise mutually recursive
      objects. However, to achieve it I use lazy values. Not as pretty as using records, but, well, there are good and bad points to each anyway.



      Something like:



      class ['self] a_class name otherLZ =

      (object (_: 'self)

      method name: string = name

      method other: 'self = Lazy.force otherLZ

      end)



      let x, y =

      let rec x = lazy (new a_class "x" y) and y = lazy (new a_class "y" x) in

      Lazy.force x, Lazy.force y;;



      Printf.printf "%s -> %s; %s -> %s\n" x#name x#other#name y#name y#other#name;;


      Stefan

      To: ocaml_beginners@yahoogroups.com
      From: francesca.lompa@...
      Date: Mon, 10 Jan 2011 16:48:58 +0000
      Subject: "ocaml_beginners"::[] Re: Instanciating mutually recursive classes.






























      Thanks for your alternative.



      Actually, this is not a big deal for my application, I was asking so as to learn the ocaml object features.



      > May be, but you can't: ocaml don't let half initialized value be. So

      > before initializing myfoo, you need the mybar value to be

      > initialized. And before initializing mybar, you need to initialize

      > myfoo: This cannot be done.



      However, there is such a feature as far as records are concerned :



      type foo = { bar_field : bar } and bar = { foo_field : foo} ;;

      let rec f = { bar_field = b} and b = {foo_field = f};;



      which results in a circular-like structure, which is what I want



      So, the internal Ocaml object implementation is rather similar to modules than records, which explain the different capabilities, isn't it ? Or maybe there is no duality at all?



      Thanks


















      [Non-text portions of this message have been removed]
    • Rémi Vanicat
      ... You cannot add initializer to a record, so the problem to know when to run those initializer don t exists. Objects (and module) are like a record, but that
      Message 2 of 5 , Jan 10, 2011
      • 0 Attachment
        "francesca.lompa" <francesca.lompa@...> writes:

        > Thanks for your alternative.
        >
        > Actually, this is not a big deal for my application, I was asking so as to learn the ocaml object features.
        >
        >
        >> May be, but you can't: ocaml don't let half initialized value be. So
        >> before initializing myfoo, you need the mybar value to be
        >> initialized. And before initializing mybar, you need to initialize
        >> myfoo: This cannot be done.
        >
        > However, there is such a feature as far as records are concerned :
        >
        > type foo = { bar_field : bar } and bar = { foo_field : foo} ;;
        > let rec f = { bar_field = b} and b = {foo_field = f};;
        >
        > which results in a circular-like structure, which is what I want

        You cannot add initializer to a record, so the problem to know when to
        run those initializer don't exists. Objects (and module) are like a
        record, but that may have also contain code that will be run at
        initialization time. So one have to know when those code have to be run
        and both class need to be initialized after the other. Ocaml chose here
        (as often) to err on the side of safety, and to refuse to try it.


        --
        Rémi Vanicat
      Your message has been successfully submitted and would be delivered to recipients shortly.