# A \`use\`-like syntax

**URL:** <https://discuss.ocaml.org/t/a-use-like-syntax/2143>\
**Category:** Learning\
**Created:** [June 14, 2018, 2:25am UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143 "2018-06-14T02:25:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![abdillah](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@abdillah](https://discuss.ocaml.org/u/abdillah)\
**Post date:** [June 14, 2018, 2:25am UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/1 "2018-06-14T02:25:36Z")

</div>

Hello, I came from Rust and found out that OCaml is a nice language.

I also found that in OCaml, module can be imported through linking and the namespace can be shortened using `open` syntax. In which, Rust equivalent of `open Module` syntax is like `use module::*`, shortening all entities under the module.

In OCaml, any specific syntax for picking some entity instead?  
I think of `let`-ing the namespace, but this kind of verbose, i.e. `let Sub = Module.Sub`.

---

<div class="post-metadata">

**Author:** ![thizanne](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/thizanne/32/764_2.png) [@thizanne](https://discuss.ocaml.org/u/thizanne)\
**Post date:** [June 14, 2018, 2:42am UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/2 "2018-06-14T02:42:31Z")

</div>

There is no way to “partially open” a module in OCaml, but a common idiome is to rename Module to a shorter name with `module M = Module`. Then you write `M.something` instead of `Module.something`.

---

<div class="post-metadata">

**Author:** ![abdillah](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@abdillah](https://discuss.ocaml.org/u/abdillah)\
**Post date:** [June 14, 2018, 9:13am UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/3 "2018-06-14T09:13:28Z")

</div>

Hey, this one is compile-able! (the let-binding I thought before wasn’t compile)

But, is it commonly used?

```auto
module type Sig = sig 
  val a : int 
  module Nested : sig 
    val b : int 
  end 
end
;;

module Mod : Sig = struct
  let a = 1
  module Nested = struct 
    let b = 2 
  end
end
;;

module Nested = Mod.Nested ;;

print_int Nested.b ;;

```

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [June 14, 2018, 12:13pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/4 "2018-06-14T12:13:59Z")

</div>

> [@thizanne](#):
>
> There is no way to “partially open” a module in OCaml, but a common idiome is to rename Module to a shorter name with `module M = Module` . Then you write `M.something` instead of `Module.something` .

One can also open a module locally, so that all the names are visible only within a given `let open`, or within a given `Module.( ... )` construct.

---

<div class="post-metadata">

**Author:** ![atavener](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@atavener](https://discuss.ocaml.org/u/atavener)\
**Post date:** [June 14, 2018, 2:22pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/5 "2018-06-14T14:22:50Z")

</div>

Absolutely, opening nested modules is used.

Sometimes I create submodules specifically for convenient inclusion of parts. Like Vec3.Ops, which has infix operators working on the Vec3.t type. I generally avoid full `open`, prefering local opens for most code, as perry mentions. This way it’s immediately clear where most functions are from, even if there is mixed use. `Vec3.ortho_proj v_desired (v /| dist)` – here, the vector-division-by-scalar is from the opened Vec3.Ops, while I keep the path to ortho\_proj explicit.

It’s also often convenient that the local open syntax works for any bracketed group, like lists or arrays, as well as general parenthesized blocks. One of my favorites is that you can scope function arguments `Glt.Tx.(Named.rgba, load byte4 dim rgba)` – here, the Glt (OpenGL tools) Tx (Texture) submodule has `Named.rgba`, `load`, and `byte4` accessed from its scope (`dim` and `rgba` are local to the calling function). This keeps expression terse (ie. without prefixing everything with `Glt.Tx`), but still reasonably explicit, unlike opening several namespaces (where did _that_ come from!?). This issue bugs me a lot when I deal with C++ code, where it would be nice to have class-local definitions/enums, but they impose repeated prefixing for each one.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [June 14, 2018, 5:51pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/6 "2018-06-14T17:51:51Z")

</div>

> [@abdillah](#):
>
> But, is it commonly used?

I don’t know about commonly, but in our codebase we use it all the time, just like you’d have `from foo import bar` in Python. It is a bit unwieldy if you want to alias multiple things but oh well.

---

<div class="post-metadata">

**Author:** ![brendanzab](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/brendanzab/32/2927_2.png) [@brendanzab](https://discuss.ocaml.org/u/brendanzab)\
**Post date:** [January 13, 2022, 11:20am UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/7 "2022-01-13T11:20:01Z")

</div>

This is something I was looking for today! I looked in the manual and tutorials etc. and was surprised that it wasn’t a feature. My original question on discord was:

> In OCaml, is there a way to import only specific thing from a module, as opposed to opening the entire module? Kind of like in Rust:
> 
> ```rust
> use foo::{bar, baz};
> 
> ```
> 
> Or Elm:
> 
> ```elm
> import Html exposing (HTML, text)
> 
> ```

Hopefully if others search for similar things might now be able to find this discussion (it took me a bit to find personally).

Somebody also posted a ppx that implements something similar, which might be of use for some people (although not as nice as it being built-in): [GitHub - johnyob/ppx-open: A ppx rewriter that provides idiomatic selective `open`s in OCaml.](https://github.com/johnyob/ppx-open)

---

<div class="post-metadata">

**Author:** ![alainfrisch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/alainfrisch/32/629_2.png) [@alainfrisch](https://discuss.ocaml.org/u/alainfrisch)\
**Post date:** [January 13, 2022, 2:21pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/8 "2022-01-13T14:21:33Z")

</div>

It’s too heavy, but the following technically works:

```ocaml
open (struct open List let map = map and filter = filter end);;

```

(parentheses just “for clarity”)

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 13, 2022, 3:02pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/9 "2022-01-13T15:02:23Z")

</div>

I think someone mentioned earlier that this is not really idiomatic in OCaml, it’s more common practice to alias the module to a convenient short name and use items with their module prefix. But, it’s still fairly easy to do if you really need it:

```auto
let (filter, map) = List.(filter, map)

```

Or even just:

```auto
let filter = List.filter
let map = List.map

```

---

<div class="post-metadata">

**Author:** ![ifazk](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ifazk/32/836_2.png) [@ifazk](https://discuss.ocaml.org/u/ifazk)\
**Post date:** [January 13, 2022, 5:19pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/10 "2022-01-13T17:19:56Z")

</div>

If you don’t mind writing out the types, there’s also

```auto
open (List : sig val hd : 'a list -> 'a end)

```

There are also ways of removing/overriding things from a module before importing, including submodules, but it’s syntactically a little heavy. See [this excellent post](http://gallium.inria.fr/blog/overriding-submodules/) by @gasche.

---

<div class="post-metadata">

**Author:** ![Alistair](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/alistair/32/3477_2.png) [@Alistair](https://discuss.ocaml.org/u/Alistair)\
**Post date:** [January 15, 2022, 3:18pm UTC](https://discuss.ocaml.org/t/a-use-like-syntax/2143/11 "2022-01-15T15:18:55Z")

</div>

`ppx-open` uses something like this internally. Namely:

```ocaml
[%%open: {| List.(map, filter) |}];;

```

is re-written to

```ocaml
open (struct let map = List.map let filter = List.filter end);;

```
