Last active
July 4, 2022 22:23
-
-
Save dsyme/bfed2eed788c7ba58ccc to your computer and use it in GitHub Desktop.
Naked type aliases can add and name constraints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) | |
Let's discuss on discussion thread please https://github.com/fsharp/fslang-design/blob/main/RFCs/FS-1124-interfaces-with-static-abstract-members.md
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fsharp/fslang-suggestions#456