最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ocaml - How can I open List without importing the type List.t? - Stack Overflow

programmeradmin6浏览0评论

I'm using OCaml 5.3.0.

I like to write open List at the top of my source files, since I use list functions so frequently that I don't want to have to prefix them as e.g. List.length or even L.length. However, that pulls the declaration type 'a t = 'a list into my namespace. That has an annoying side effect: when VS Code displays a list type, it uses the t name so that I see e.g. int t rather than int list.

So is there some way I can open List without pulling in t? In Haskell I could write import List hiding (t), but I don't see an equivalent in OCaml.

I'm using OCaml 5.3.0.

I like to write open List at the top of my source files, since I use list functions so frequently that I don't want to have to prefix them as e.g. List.length or even L.length. However, that pulls the declaration type 'a t = 'a list into my namespace. That has an annoying side effect: when VS Code displays a list type, it uses the t name so that I see e.g. int t rather than int list.

So is there some way I can open List without pulling in t? In Haskell I could write import List hiding (t), but I don't see an equivalent in OCaml.

Share Improve this question edited Mar 27 at 6:42 Chris 36.9k6 gold badges33 silver badges55 bronze badges asked Mar 27 at 6:28 Adam DingleAdam Dingle 3,6721 gold badge18 silver badges14 bronze badges 3
  • As an aside, there may be elements of the XY problem here. It would be interesting to see an example of this code that uses a very large number of List module functions. – Chris Commented Mar 27 at 6:47
  • 2 IMHO such global openings are a bad idea (except to import some notations). What is length then? It may just as well mean the length of an array, or string, or of anything else. It is unnatural to define this function name for lists specifically, and it is error-prone. What if you also open Array? Also, how can I easily identify where length comes from, without advanced editor support for OCaml? Short aliases like module L = List are much better. – Maëlan Commented Mar 27 at 15:03
  • I have thousands of lines of OCaml code (a theorem proving system) where I use lists in zillions of places. As I wrote in my original post, I really don't want to have prefix every list function with L. In many other languages (such as Haskell) common list functions are available in the standard library in an unprefixed fashion, and I'm used to that. However, I understand that in the OCaml world the conventions are a bit different, and writing e.g. L.length is normal. Perhaps I will eventually switch to those conventions, but am not yet completely convinced that I want to. :) – Adam Dingle Commented Mar 28 at 7:26
Add a comment  | 

2 Answers 2

Reset to default 1

I found one possible way, which is a bit tricky. I can make a source file util.ml and put this into it:

include (List : module type of List with type 'a t := 'a list)
open List

That says to include List in the Util module, but replace the type 'a t with 'a list. Now Util will contain all the List functions but not List.t, so in other source files I can write

open Util

You could try to alias your type t before you open List and then revert that afterwards. I don't use VS Code, so I don't know for sure if this will work.

Something like:

type t = ...
type t' = t

open List

type t = t'

You may also be looking for more localized versions of opening a module.

E.g.

let double_and_sum lst =
  List.(
    lst 
    |> map (( * ) 2) 
    |> fold_left (+) 0
  )

Or:

let double_and_sum lst =
  let open List in
  lst 
  |> map (( * ) 2) 
  |> fold_left (+) 0

In both of these cases it's reasonably clear where the functions are coming from.

发布评论

评论列表(0)

  1. 暂无评论