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

Re: "ocaml_beginners"::[] Records

Expand Messages
  • Virgile Prevosto
    Le lundi 31 mai, à 21h30 -0500, ... What s wrong with the previously mentionned module encapsulation ? # module A = struct type a = {b:int} end;; module A :
    Message 1 of 7 , Jun 1, 2004
      Le lundi 31 mai, à 21h30 -0500,
      Jeffrey J. Cook a écrit:

      > Aside from using variants/classes/etc, the best solution I can think
      > of is to use accessor functions for your field arguments:

      What's wrong with the previously mentionned module encapsulation ?

      # module A = struct type a = {b:int} end;;
      module A : sig type a = { b : int; } end
      # module B = struct type b = { b:string} end;;
      module B : sig type b = { b : string; } end
      # let f x = x.A.b;;
      val f : A.a -> int = <fun>
      # let g x = x.B.b;;
      val g : B.b -> string = <fun>

      If you don't want to systematically use prefixed field names, you can
      use the 'open' directive (note that this will only work for one type
      at a time, but the other one can still be accessed through the fully
      qualified name). You can of course use several open directives in the
      same session:

      # open A;; (* x.b refers to the a type*)
      # let f x = x.b;;
      val f : A.a -> int = <fun>
      # open B;; (* now switch to b type *)
      # let g x = x.b;;
      val g : B.b -> string = <fun>
      # open A;; (* let's go to a again *)
      # let f x = x.b;;
      val f : A.a -> int = <fun>


      --
      E tutto per oggi, a la prossima volta
      Virgile
    • Jeffrey J. Cook
      ... Module encapsulation is what generally should be used; I was just providing ideas for temporary hack alternatives that can be sufficient for cases that
      Message 2 of 7 , Jun 1, 2004
        On Tue, Jun 01, 2004 at 11:14:10AM +0200, Virgile Prevosto wrote:
        > What's wrong with the previously mentionned module encapsulation ?

        Module encapsulation is what generally should be used; I was just providing
        ideas for temporary 'hack' alternatives that can be sufficient for cases that
        can arise well into a large project.

        --
        Jeffrey J. Cook
        Graduate Student, Electrical Engineering
        University of Illinois at Urbana-Champaign
        jjcook@...
      Your message has been successfully submitted and would be delivered to recipients shortly.