mirage/orm

Signature generator needed for scary types

avsm opened this issue · 3 comments

avsm commented

We can improve on:

utop $ type t = { foo: int; bar: string } with orm;;
type t = { foo : int; bar : string; }
val __env__ : Orm.Sql_backend.env = [] 
val hash_of_t : t -> int = <fun>                                                                                                                                      
module Cache_t : sig type tbl type elt = t val create : string -> (tbl, elt) Orm.Sql_cache.t end
val t_cache : (Cache_t.tbl, Cache_t.elt) Orm.Sql_cache.t =
  {Orm.Sql_cache.type_name = "t"; Orm.Sql_cache.tbl = <abstr>; Orm.Sql_cache.create = <fun>; 
   Orm.Sql_cache.to_weakid = <fun>; Orm.Sql_cache.of_weakid = <fun>; 
   Orm.Sql_cache.mem = <fun>; Orm.Sql_cache.mem_weakid = <fun>; Orm.Sql_cache.add = <fun>; 
   Orm.Sql_cache.remove = <fun>; Orm.Sql_cache.replace = <fun>; Orm.Sql_cache.dump = <fun>}
val type_of_t : Dyntype.Type.t = Dyntype.Type.Ext ("t", Dyntype.Type.Dict (`R, [("foo", `RO, Dyntype.Type.Int (Some 63)); ("bar", `RO, Dyntype.Type.String)]))
val value_of_t : id_seed:Orm.Sql_backend.state -> t -> Dyntype.Value.t = <fun>
val t_of_value : Orm.Sql_get.V.t -> t = <fun>
module ORMID_t : Orm.Sig.ID
val t_init : string -> (t, [ `RW ]) Orm.Db.t = <fun>
val t_init_read_only : string -> (t, [ `RO ]) Orm.Db.t = <fun>
val t_save : db:(t, [ `RW ]) Orm.Db.t -> Cache_t.elt -> unit = <fun>
val t_get :
  ?foo:[< `Eq of int | `Ge of int | `Geq of int | `Le of int | `Leq of int | `Neq of int ] ->
  ?bar:[< `Contains of string | `Eq of string ] -> ?custom:(t -> bool) -> ?order_by:[> `bar | `foo ] -> (t, [< `RO | `RW ]) Orm.Db.t -> Cache_t.elt list = <fun>
val t_lazy_get :
  ?foo:[< `Eq of int | `Ge of int | `Geq of int | `Le of int | `Leq of int | `Neq of int ] ->
  ?bar:[< `Contains of string | `Eq of string ] -> ?custom:(t -> bool) -> ?order_by:[> `bar | `foo ] -> (t, [< `RO | `RW ]) Orm.Db.t -> unit -> Cache_t.elt option =
  <fun>
val t_get_by_id : id:[< `Eq of ORMID_t.t ] -> (t, [< `RO | `RW ]) Orm.Db.t -> Cache_t.elt = <fun>
val t_delete : ?recursive:bool -> db:(t, [ `RW ]) Orm.Db.t -> Cache_t.elt -> unit = <fun>
val t_id : db:(t, [< `RO | `RW ]) Orm.Db.t -> Cache_t.elt -> ORMID_t.t = <fun>
module ORM_t :
  sig
    type t = t
    type id = ORMID_t.t
    type 'get_result get_params =
        ?foo:[ `Eq of int | `Ge of int | `Geq of int | `Le of int | `Leq of int | `Neq of int ] -> ?bar:[ `Contains of string | `Eq of string ] -> 'get_result
    type order_by = [ `bar | `foo ]
    val init : string -> (t, [ `RW ]) Orm.Db.t
    val init_read_only : string -> (t, [ `RO ]) Orm.Db.t
    val save : db:(t, [ `RW ]) Orm.Db.t -> t -> unit
    val get : (?custom:(t -> bool) -> ?order_by:order_by -> (t, [< `RO | `RW ]) Orm.Db.t -> t list) get_params
    val lazy_get : (?custom:(t -> bool) -> ?order_by:order_by -> (t, [< `RO | `RW ]) Orm.Db.t -> unit -> t option) get_params
    val get_by_id : id:[ `Eq of id ] -> (t, [< `RO | `RW ]) Orm.Db.t -> t
    val delete : ?recursive:bool -> db:(t, [ `RW ]) Orm.Db.t -> t -> unit
    val id : db:(t, [< `RO | `RW ]) Orm.Db.t -> t -> id

Not sure what we can generate here to improve the output.

avsm commented

We could improve this quite a bit I think:

  • hide the ORM_t module entirely
  • hide Cache_t and t_cache and __env__ value bindings: it's a little dodgy that this is exposed at all. It could be hidden properly by doing what Dyntype does and doing all the top-level binds with a assignment to a tuple of functions.
  • I don't know if the signature generator magically works with the top-level, but I presume it does or else abstraction would be pretty broken there.

Still, at least it works at all! :-)

hide the ORM_t module entirely
hide Cache_t and t_cache and env value bindings: it's a little dodgy that this is exposed at all. It could be hidden properly by doing what Dyntype does and doing all the top-level binds with a assignment to a tuple of functions.
I don't know if the signature generator magically works with the top-level, but I presume it does or else abstraction would be pretty broken there.

Have any of these been implemented, or perhaps another approach taken to make the signatures generated more readable?