From: Anthony Tavener <anthony.tavener@gmail.com>
To: Martin Jambon <martin.jambon@ens-lyon.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Nested module exposing type from parent?
Date: Wed, 2 Nov 2011 19:04:44 -0600 [thread overview]
Message-ID: <CAN=ouMRDJPfsiTf-ZrXTzkB1GrEds_bxs+nKp0+r8CrT3boFXA@mail.gmail.com> (raw)
In-Reply-To: <4EB1E325.4020207@ens-lyon.org>
[-- Attachment #1: Type: text/plain, Size: 2655 bytes --]
Aha! That's more like what I was going for, Martin.
I didn't realize you could equate types in a chain like that, permitting
the definition of structure but also equality to another existing type.
That's good stuff.
This list, and OCaml, often amaze me... solutions keep getting better. :)
On Wed, Nov 2, 2011 at 6:41 PM, Martin Jambon <martin.jambon@ens-lyon.org>wrote:
> On 11/02/2011 12:41 PM, Anthony Tavener wrote:
> > I've been struggling with this occasionally...
> >
> > I'm using nested modules to "open" access to select features of a
> > module. My problem is I can't find a way to *expose* types in the parent
> > module through such nested modules.
> >
> > A simplified example of what I'm looking at:
> >
> > module Vec = struct
> >
> > type t = { x: int; y: int }
> > let make x y = {x;y}
> > let add a b = {x=a.x+b.x; y=a.y+b.y}
> >
> > module Type =
> > (* something which has type t = Vec.t,
> > * with exposed structure when "open"ed.
> > * Also note that Vec is not really an
> > * explicit module like this; instead it
> > * is implemented in vec.ml <http://vec.ml> *)
> > end
> >
> > Example usage...
> >
> > let n = Vec.make 2 5
> > open Vec.Type
> > let m = {x=1;y=2}
> > Vec.add m n
>
> I hope I understand the problem correctly.
>
> In order for that code to work, you can do this:
>
> module Vec = struct
>
> type t = { x: int; y: int }
> let make x y = {x;y}
> let add a b = {x=a.x+b.x; y=a.y+b.y}
>
> module Type = struct
> type t' = t = { x: int; y: int }
> type t = t' = { x: int; y: int }
> (* something which has type t = Vec.t,
> * with exposed structure when "open"ed.
> * Also note that Vec is not really an
> * explicit module like this; instead it
> * is implemented in vec.ml <http://vec.ml> *)
> end
> end
>
>
> Or more simply:
>
>
> module Vec = struct
>
> module Type = struct
> type t = { x: int; y: int }
> (* something which has type t = Vec.t,
> * with exposed structure when "open"ed.
> * Also note that Vec is not really an
> * explicit module like this; instead it
> * is implemented in vec.ml <http://vec.ml> *)
> end
>
> type t = Type.t = { x: int; y: int }
> let make x y = {x;y}
> let add a b = {x=a.x+b.x; y=a.y+b.y}
>
> end
>
>
> Now you can open either Vec or Vec.Type and have direct access to the
> record fields.
>
>
> Martin
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
[-- Attachment #2: Type: text/html, Size: 4144 bytes --]
prev parent reply other threads:[~2011-11-03 1:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-02 19:41 Anthony Tavener
2011-11-02 20:19 ` Vincent Aravantinos
[not found] ` <CAN=ouMTApZjpU-CaZtdL4njXtmtRu++7fzJBJL3w3FRcHfjtSA@mail.gmail.com>
[not found] ` <CAN=ouMS9rqqNR3KgCBnnjC_HcMrUnftVw643mkmhC_vrpXfv1A@mail.gmail.com>
2011-11-02 21:14 ` Fwd: " Anthony Tavener
2011-11-02 23:01 ` Gabriel Scherer
2011-11-03 0:06 ` Vincent Aravantinos
2011-11-03 1:03 ` Anthony Tavener
2011-11-03 0:41 ` Martin Jambon
2011-11-03 1:04 ` Anthony Tavener [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN=ouMRDJPfsiTf-ZrXTzkB1GrEds_bxs+nKp0+r8CrT3boFXA@mail.gmail.com' \
--to=anthony.tavener@gmail.com \
--cc=caml-list@inria.fr \
--cc=martin.jambon@ens-lyon.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox