package provider

  1. Overview
  2. Docs

Module Provider.TraitSource

Sourcetype ('t, 'module_type, 'tag) t

Think of a Trait as a way to designate the signature of a module that contains enough functions to support some functionality. The type t allows to identify a Trait within the provider system. The name was inspired from the Rust programming language construct of the same name.

  • 't is the internal type on which the provider traits operate.
  • 'module_type is the signature of a module implementing the Trait.
  • 'tag is the tag (or tags) indicating the supported Trait(s). It's a phantom type designed to help using lookup correctly.

'module_type is typically expected to be a module type, but it doesn't have too (functions, constants are fine too, etc.).

Creating traits

Traits are abstract and must be created using the following functors. The most common one is Create. It is to be used when the trait is defined by a module type with a single type t. For example:

  module type Show = sig
    type t

    val show : t -> string
  end

  module Show : sig
    val t : ('a, (module Show with type t = 'a), [> `Show ]) Provider.Trait.t
  end = Provider.Trait.Create (struct
      type 'a module_type = (module Show with type t = 'a)
    end)

The other functors are reserved for less common cases. The number suffix indicates the number of parameters of the module_type type, each of which must be present and injective in X.t. We added one extra parameter to X.t to allow for more flexibility in what can be expressed, but not all parameters have to be used.

Sourcemodule Create (X : sig ... end) : sig ... end
Sourcemodule Create0 (X : sig ... end) : sig ... end
Sourcemodule Create1 (X : sig ... end) : sig ... end
Sourcemodule Create2 (X : sig ... end) : sig ... end

Dump & debug

Sourcemodule Info : sig ... end

Displaying debugging information about a trait.

Sourceval info : (_, _, _) t -> Info.t

Indexation

Sourcemodule Uid : sig ... end
Sourceval uid : (_, _, _) t -> Uid.t

A uid is particularly useful when you need to quickly look up or sort Traits, as it provides a consistent and unique way to identify each Trait. You can use it to manipulate Traits within container structures, making it easier to store, retrieve, and compare Traits at runtime.

Trait uniq ids are computed with Obj.Extension_constructor.id, applied to the constructors of the variant type Trait.t, making them valid only for the lifetime of the running program.

Sourceval same : (_, _, _) t -> (_, _, _) t -> bool