-
-
Save dsyme/bfed2eed788c7ba58ccc to your computer and use it in GitHub Desktop.
// This F# language suggestion wants a way to name collections of constraints: | |
// http://fslang.uservoice.com/forums/245727-f-language/suggestions/8509687-add-constraints-as-a-language-construct | |
// | |
// This is a type alias X<T> = T, so X<int> = int etc. | |
type X<'T> = 'T | |
// This is a type alias X<T> = T which adds a constraint | |
type WithStruct<'T when 'T : struct> = 'T | |
// Use it like this: | |
let f1 (x: WithStruct<'T>) : 'T = x | |
// This shows a type alias can imply multiple named constraintsm which gives a way of naming collections of constraints: | |
type WithFG< ^T when ^T : (member F : int -> unit) | |
and ^T : (member G : int -> unit) > = ^T | |
// Use it like this: | |
let inline f2 (x: WithFG< ^T >) = | |
(^T : (member F : int -> unit) (x, 1)) | |
(^T : (member G : int -> unit) (x, 2)) | |
What's the advantage of using "constraint aliases" over
let inline fnF x n = (^T : (member F : int -> unit) (x, n))
let inline fnG x n = (^T : (member G : int -> unit) (x, n))
let inline f3 x =
fnF x 1
fnG x 2
every time (x: WithFG< ^T >)
is used in an argument the function body will be polluted with (^T : (member F : int -> unit) (x, 1))
to take advantage of the SRTP,
Thanks Don for sharing the Gist but unfortunately the type alias trick does not solve the problem for purely static member constraints. In the following example for instance I want to express the same set of constraints for both ^T1 and ^T2 without having to repeat them twice.
type Combine< ^T1, ^T2 when
^T1 : (static member print : string -> unit)
and ^T1 : (static member flush : unit -> unit)
and ^T2 : (static member print : string -> unit)
and ^T2 : (static member flush : unit -> unit)
> =
static member inline printAndFlushBoth m =
(^T1:(static member print : string -> unit) m)
(^T2:(static member print : string -> unit) m)
(^T1:(static member flush : unit -> unit) ())
(^T2:(static member flush : unit -> unit) ())
Following your trick I could define a type alias encoding the two constraints (write and flush) but my constraints being entirely static I have no way to refer to the type alias it in a type annotation since there exist no variable of static type.
Do you have another trick up your sleeve for the above example?
Another benefit of named constraints is that the 'comparison' and 'equality' constraints could be defined in the F# core library instead of being hard-coded in the language.
An alternative suggestion would be to add support for generic type constraints of the form ^T :> GenericType<_>
. Together with the above trick that would allow me to write:
type Printer< ^T when
^T : (static member print : string -> unit)
and ^T : (static member flush : unit -> unit) > = ^T
type Combine< ^T1, ^T2 when ^T1 :> Printer<_> and ^T2 :> Printer<_> > =
static member inline printAndFlushBoth m =
(^T1:(static member print : string -> unit) m)
(^T2:(static member print : string -> unit) m)
(^T1:(static member flush : unit -> unit) ())
(^T2:(static member flush : unit -> unit) ())
which currently gives me an error:
error FS0698: Invalid constraint: the type used for the constraint is sealed, which means the constraint could only be satisfied by at most one solution
This would greatly help me.
Let's discuss on discussion thread please https://github.com/fsharp/fslang-design/blob/main/RFCs/FS-1124-interfaces-with-static-abstract-members.md
they can name, but don't seem to allow to use without restating the constraint:
So I'm still looking for how it is helping?