caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Importing specific names
@ 2012-10-26 11:26 José Romildo Malaquias
  2012-10-26 11:26 ` David House
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: José Romildo Malaquias @ 2012-10-26 11:26 UTC (permalink / raw)
  To: caml-list

Hello.

It seems that the expression ocaml "open module-path" exposes all names
exported by module-path.

Is there any way of exposing only a restrict set of names from a
module-path, instead of all names?

Romildo

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 [Caml-list] Importing specific names José Romildo Malaquias
@ 2012-10-26 11:26 ` David House
  2012-10-26 11:30   ` David House
  2012-10-26 17:45   ` José Romildo Malaquias
  2012-10-26 13:55 ` Matthieu Dubuget
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 10+ messages in thread
From: David House @ 2012-10-26 11:26 UTC (permalink / raw)
  To: caml-list

If you want to list the things you want, rather than listing the
things you don't want, what's wrong with the following?

let x = Module.x
let y = Module.y
...

You could abbreviate that somewhat:

include (struct
  open Module
  let x = x
  let y = y
  ...
end)

On Fri, Oct 26, 2012 at 12:26 PM, José Romildo Malaquias
<j.romildo@gmail.com> wrote:
> Hello.
>
> It seems that the expression ocaml "open module-path" exposes all names
> exported by module-path.
>
> Is there any way of exposing only a restrict set of names from a
> module-path, instead of all names?
>
> Romildo
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 ` David House
@ 2012-10-26 11:30   ` David House
  2012-10-26 17:45   ` José Romildo Malaquias
  1 sibling, 0 replies; 10+ messages in thread
From: David House @ 2012-10-26 11:30 UTC (permalink / raw)
  To: caml-list

I should add: although this is possible, I generally find it easier to
read code that has been written with as few opens as possible.

If you find you are using things from Module a lot, you might consider
creating a module alias:

module Mod = Very_long_module_name

You can then say Mod.foo, Mod.bar etc. This makes it obvious where foo
and bar come from, but you don't have to sacrifice readability by
typing such a long module name every time.

(Of course, there is an art to picking module abbreviations as well; I
find it best to pick a short abbreviation which holds at least some
meaning, rather than just "let module M = ...". E.g.:
  module Str = My_super_duper_string_module
Rather than:
  module S = My_super_duper_string_module
)

Yet another option is to do a local module alias ("let module Mod =
... in"), or a local open ("let open ... in"), in the function(s)
where you need access to things from Module. This keeps things as
localised as possible, which again increases readability. (You only
have to remember the meaning of a module alias for a very short time,
rather than for the scope of the whole module.)

On Fri, Oct 26, 2012 at 12:26 PM, David House <dhouse@janestreet.com> wrote:
> If you want to list the things you want, rather than listing the
> things you don't want, what's wrong with the following?
>
> let x = Module.x
> let y = Module.y
> ...
>
> You could abbreviate that somewhat:
>
> include (struct
>   open Module
>   let x = x
>   let y = y
>   ...
> end)
>
> On Fri, Oct 26, 2012 at 12:26 PM, José Romildo Malaquias
> <j.romildo@gmail.com> wrote:
>> Hello.
>>
>> It seems that the expression ocaml "open module-path" exposes all names
>> exported by module-path.
>>
>> Is there any way of exposing only a restrict set of names from a
>> module-path, instead of all names?
>>
>> Romildo
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 [Caml-list] Importing specific names José Romildo Malaquias
  2012-10-26 11:26 ` David House
@ 2012-10-26 13:55 ` Matthieu Dubuget
  2012-10-26 17:51   ` José Romildo Malaquias
  2012-10-26 14:48 ` Edgar Friendly
  2012-10-26 15:26 ` Didier Cassirame
  3 siblings, 1 reply; 10+ messages in thread
From: Matthieu Dubuget @ 2012-10-26 13:55 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 654 bytes --]

Le 26/10/2012 13:26, José Romildo Malaquias a écrit :
>  Hello.
>
>  It seems that the expression ocaml "open module-path" exposes all names
>  exported by module-path.
>
>  Is there any way of exposing only a restrict set of names from a
>  module-path, instead of all names?
>
>  Romildo
>

Is this what you are looking for?

# module type RS = sig
    val length : string -> int
   end;;
module type RS = sig val length : string -> int end
# module ReducedString:RS = String;;
module ReducedString : RS
# ReducedString.length "coco";;
- : int = 4
# ReducedString.uppercase "coco";;
Error: Unbound value ReducedString.uppercase

Salutations

Matthieu


[-- Attachment #2: Type: text/html, Size: 1181 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 [Caml-list] Importing specific names José Romildo Malaquias
  2012-10-26 11:26 ` David House
  2012-10-26 13:55 ` Matthieu Dubuget
@ 2012-10-26 14:48 ` Edgar Friendly
  2012-10-26 15:20   ` Yaron Minsky
  2012-10-26 17:56   ` José Romildo Malaquias
  2012-10-26 15:26 ` Didier Cassirame
  3 siblings, 2 replies; 10+ messages in thread
From: Edgar Friendly @ 2012-10-26 14:48 UTC (permalink / raw)
  To: caml-list

On 10/26/2012 7:26 AM, José Romildo Malaquias wrote:
> Hello.
>
> It seems that the expression ocaml "open module-path" exposes all names
> exported by module-path.
>
> Is there any way of exposing only a restrict set of names from a
> module-path, instead of all names?
>
> Romildo
>
If I want to import only a few values from a module, I often use 
something like this:

   let (a,b,c) = Module.(a,b,c)

E.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 14:48 ` Edgar Friendly
@ 2012-10-26 15:20   ` Yaron Minsky
  2012-10-26 17:56   ` José Romildo Malaquias
  1 sibling, 0 replies; 10+ messages in thread
From: Yaron Minsky @ 2012-10-26 15:20 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

Ah, that's great!  I wish I could do it for binding sub-modules too!

y

On Fri, Oct 26, 2012 at 10:48 AM, Edgar Friendly <thelema314@gmail.com> wrote:
> On 10/26/2012 7:26 AM, José Romildo Malaquias wrote:
>>
>> Hello.
>>
>> It seems that the expression ocaml "open module-path" exposes all names
>> exported by module-path.
>>
>> Is there any way of exposing only a restrict set of names from a
>> module-path, instead of all names?
>>
>> Romildo
>>
> If I want to import only a few values from a module, I often use something
> like this:
>
>   let (a,b,c) = Module.(a,b,c)
>
> E.
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 [Caml-list] Importing specific names José Romildo Malaquias
                   ` (2 preceding siblings ...)
  2012-10-26 14:48 ` Edgar Friendly
@ 2012-10-26 15:26 ` Didier Cassirame
  3 siblings, 0 replies; 10+ messages in thread
From: Didier Cassirame @ 2012-10-26 15:26 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 715 bytes --]

One way to do that is to define the signature you need, and restrict the
module with it.

module Restricted = (Module : sig val a ... end )

open Restricted

didier

2012/10/26 José Romildo Malaquias <j.romildo@gmail.com>

> Hello.
>
> It seems that the expression ocaml "open module-path" exposes all names
> exported by module-path.
>
> Is there any way of exposing only a restrict set of names from a
> module-path, instead of all names?
>
> Romildo
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/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: 1311 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 11:26 ` David House
  2012-10-26 11:30   ` David House
@ 2012-10-26 17:45   ` José Romildo Malaquias
  1 sibling, 0 replies; 10+ messages in thread
From: José Romildo Malaquias @ 2012-10-26 17:45 UTC (permalink / raw)
  To: David House; +Cc: caml-list

On Fri, Oct 26, 2012 at 12:26:02PM +0100, David House wrote:
> If you want to list the things you want, rather than listing the
> things you don't want, what's wrong with the following?
> 
> let x = Module.x
> let y = Module.y
> ...
> 
> You could abbreviate that somewhat:
> 
> include (struct
>   open Module
>   let x = x
>   let y = y
>   ...
> end)

That does not seem to work with types:

  (* test.ml *)
  include (struct
    open Lexing
    type position = position
  end)

# #use "test.ml";;
File "test.ml", line 4, characters 7-26:
Error: The type abbreviation position is cyclic

> On Fri, Oct 26, 2012 at 12:26 PM, José Romildo Malaquias
> <j.romildo@gmail.com> wrote:
> > Hello.
> >
> > It seems that the expression ocaml "open module-path" exposes all names
> > exported by module-path.
> >
> > Is there any way of exposing only a restrict set of names from a
> > module-path, instead of all names?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 13:55 ` Matthieu Dubuget
@ 2012-10-26 17:51   ` José Romildo Malaquias
  0 siblings, 0 replies; 10+ messages in thread
From: José Romildo Malaquias @ 2012-10-26 17:51 UTC (permalink / raw)
  To: Matthieu Dubuget; +Cc: caml-list

On Fri, Oct 26, 2012 at 03:55:43PM +0200, Matthieu Dubuget wrote:
> Le 26/10/2012 13:26, José Romildo Malaquias a écrit :
> >  Hello.
> >
> >  It seems that the expression ocaml "open module-path" exposes all names
> >  exported by module-path.
> >
> >  Is there any way of exposing only a restrict set of names from a
> >  module-path, instead of all names?
> 
> Is this what you are looking for?
> 
> # module type RS = sig
>     val length : string -> int
>    end;;
> module type RS = sig val length : string -> int end
> # module ReducedString:RS = String;;
> module ReducedString : RS
> # ReducedString.length "coco";;
> - : int = 4
> # ReducedString.uppercase "coco";;
> Error: Unbound value ReducedString.uppercase

This has the disadvantage to explicitly include the type signature of
values, and probably the type definition of types. There sould be easier
ways, where only the relevant names would have to be mentioned.

Romildo

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Importing specific names
  2012-10-26 14:48 ` Edgar Friendly
  2012-10-26 15:20   ` Yaron Minsky
@ 2012-10-26 17:56   ` José Romildo Malaquias
  1 sibling, 0 replies; 10+ messages in thread
From: José Romildo Malaquias @ 2012-10-26 17:56 UTC (permalink / raw)
  To: Edgar Friendly; +Cc: caml-list

On Fri, Oct 26, 2012 at 10:48:11AM -0400, Edgar Friendly wrote:
> On 10/26/2012 7:26 AM, José Romildo Malaquias wrote:
> > Hello.
> >
> > It seems that the expression ocaml "open module-path" exposes all names
> > exported by module-path.
> >
> > Is there any way of exposing only a restrict set of names from a
> > module-path, instead of all names?
> >
> > Romildo
> >
> If I want to import only a few values from a module, I often use 
> something like this:
> 
>    let (a,b,c) = Module.(a,b,c)

What about importing algebraic types with its constructors or record
types with its field names, without repeating the type definition? Would
that be possible?

For instance, I want to import the record type

  type position = {
       pos_fname :string;
       pos_lnum :int;
       pos_bol :int;
       pos_cnum :int;
  }

from the Lexing module.

Romildo

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-10-26 18:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-26 11:26 [Caml-list] Importing specific names José Romildo Malaquias
2012-10-26 11:26 ` David House
2012-10-26 11:30   ` David House
2012-10-26 17:45   ` José Romildo Malaquias
2012-10-26 13:55 ` Matthieu Dubuget
2012-10-26 17:51   ` José Romildo Malaquias
2012-10-26 14:48 ` Edgar Friendly
2012-10-26 15:20   ` Yaron Minsky
2012-10-26 17:56   ` José Romildo Malaquias
2012-10-26 15:26 ` Didier Cassirame

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).