Immediately Opened Functors - why aren't they possible?


#1

I was playing with monads, and I was curious - why can’t we open functor module expressions? Why do we have to give them a name, or include them? Is there anything preventing us from doing this?

Code I was playing with:

module type Type  = sig type t end

module type Monad  =
  sig
    type 'a t
    val pure : 'a -> 'a t
    val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
  end

module Monad_result(T: Type) : Monad =
  struct
    type 'o t = ('o, T.t) result
    let pure x = Ok x
    let (>>=) res f =
      match res with
      | Ok o -> f o
      | Err e -> Err e
  end

(* what I have to write *)
module M = Monad_result(struct type t = int end)
open M
(* what I'd like to write *)
open Monad_result(struct type t = int end)

#2

See https://stackoverflow.com/questions/32102839/is-it-possible-to-open-or-use-a-functor-without-an-intermediate-module-in-ocaml


#3

Indeed it would be great!

Jeremy Yallop and Runhang Li presented a proposal at the OCaml Workshop 2017.
Here are the paper and the pull request: