Interface Subtype Limitations

I want to create several “tags” for several modules. A function may require several such "signs" as its input, i.e.:

type 'a x_t = < get_x : int ; .. > as 'a constraint 'a = < get_b : float ; .. >
val foo : x_t -> float 

manually compiling these features in the interface is cumbersome and error prone, but quite possible.

But in an ideal world, I should be able to use the name "trait" instead of manually composing all the necessary fields, i.e. write something like:

 type 'a x_t = < get_x : int ; .. > as 'a constraint 'a <: a_t

Unfortunately, the OCaml syntax does not allow this. This seems to be a purely syntactical limitation, so I wonder if there is a deeper reason for this?

In other words: Why can't I directly write subtype restrictions in signatures like OCaml?

edit: To clarify the use case: I have several (sometimes interdependent) modules that provide functionality in a general general state. These modules must be composite and loosely coupled (i.e. they require that their part of the global state satisfy their needs, and therefore I can always expand the state with the help of new functions). To achieve this, the state is encapsulated in an object that provides multiple lenses (above I used only getters, not setters). Therefore, if I provide interface definitions for my modules, I need to describe the required lenses in the signature of my functions through restrictions on the type parameter that encodes the entire state type. Now I'm looking for a way to write these formulated signature requirements as short and readable as possible.

+1
1

, , #foo.

class type b = object
  method get_b : float
end

class type c = object
  method get_c : bool
end

type 'a x_t = < .. > as 'a 
  constraint 'a = #b
  constraint 'a = #c

:

class type e = object
  inherit b inherit c
end

(, , , ...)

+7

Source: https://habr.com/ru/post/1546562/


All Articles