ocaml-ppx / ppx_import

Less redundancy in type declarations and signatures
MIT License
89 stars 29 forks source link

import a module to re-export it #51

Closed ulugbekna closed 3 years ago

ulugbekna commented 3 years ago

One often needs to export a module imported from another module, e.g.,

(* m0.ml *)
module M = struct end 

(* m1.ml *) 
(* wants to re-export [M0.M] as [M] now: *)
module M = M0.M

I think this could be solved by this ppx by something like

[%%import M0.M] (* that could be expanded to module M = M0.M *)

My use-case is to avoid this:

module Capability = Capability
module Proto_request = Protocol.Proto_request
module Advertised_refs = Protocol.Advertised_refs
module Want = Protocol.Want
module Result = Protocol.Result
module Negotiation = Protocol.Negotiation
module Shallow = Protocol.Shallow
module Commands = Protocol.Commands
module Status = Protocol.Status
gasche commented 3 years ago

Hi @ulugbekna! I am not too enthusiastic about your feature suggestion, because to me it falls outside the scope of ppx_import. The job of ppx_import is to lookup the full definition of an OCaml object defined elsewhere, for the purpose of facilitating "derived" definitions, metaprograms that inspect the full definition. What you propose is more of a syntactic convenience that is easily expanded locally, it has a different purpose.

Two remarks:

ulugbekna commented 3 years ago

Hi Gabriel! Thanks for your reply.

I was under impression that the behavior that I described fit into the semantics of word "import" and use-case similar to keywords "import" in Scala or python.

open populates the environment with more values from Protocol than I would like to.

[%import Package.User] (* to be able to just use an alias module [User]; similar to Scala's [import Package.User] *) 

(* or *)

[%import Package.User as U] (* module U = Package.User *) 

(* or *) 

[%import Package.User.{login, password} (* let login = Package.User.login;; let password = Package.User.password *)

(* or *)

[%import Package.User.{login, password} as U (* 
  module U = struct 
    let login = Package.User.login;; let password = Package.User.password;;
  end *)
*)
gasche commented 3 years ago

I was under impression that the behavior that I described fit into the semantics of word "import" and use-case similar to keywords "import" in Scala or python.

Yes, but the purpose of ppx_import is not to provide an "import" feature as in these languages for OCaml users (or in general to provide any sort of syntactic language features), but really to enable other extensions to do meta-programming on external/imported definitions.

ulugbekna commented 3 years ago

Thanks for consideration! :-)