Allow some type declaration sub branches being annotated with meaningfull descriptions
I propose we allow to annotates some type declarations, besides DU's with the descriptions of what contextual info the underlying types are carrying Consider this DU type:
type DU =
| IdName of Id: string * Name: float
| Index of Index: int
it is possible to annotate the payload of the cases with inlined descriptions for DU's however it is not possible for Map type for example:
type SomeMapData = System.Collections.Generic.SortedDictionary<versionId: string, Map<docName: string, docId: string>>
//or in function declaration
let foo (m: Map<name: string, id: string>) =
//....
The existing way of approaching this problem in F# is to declare type alias and create the documentation for it with description of what info is implied under certain string, int or float primitive. Even then its tricky to properly describe what subtree of type declaration is exactly being described. Another approach is to create separate single DU case for each of the primitive types involved, which is often not needed.
Pros and Cons
The advantages of making this adjustment to F# are more transparency in using standard types like tuples and maps and consistency with DU types
The disadvantages of making this adjustment to F# are hard to evolve for me, this annotations are already working for DU cases payload, so it probably can be generalized.
Extra information
Estimated cost (XS, S, M, L, XL, XXL): ?
Affidavit (please submit!)
Please tick this by placing a cross in the box:
- [+] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
- [+] I have searched both open and closed suggestions on this site and believe this is not a duplicate
- [+] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
- [ ] This is not a breaking change to the F# language design
- [ ] I or my company would be willing to help implement and/or test this
For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
I don't think this has to do with F#.
The suggestion is to allow naming of generic type arguments in dotnet. Then for example the map.Keys property would have intellisense type ICollection<name: string>.
This seems like a useful suggestion but would be a very large project in dotnet.
@charlesroddie if this is the case, how it works for DU's?
DU types are generated by the F# compiler. Secondly, you're giving names to fields, not type parameters.
If you were to do Map<docName: string, docId: string>, where would that information be stored? The Map type already exists. You could theoretically use types with units of measure as type arguments.
@kerams But naming elements of tuples works in DU, even if they are generic, that info is obviously stored somewhere as metadata of type declaration. Probably the same can be made at least for type aliases.
Those aren't tuples but fields on the class generated by the F# compiler. This is a tuple, and you can't give those elements names. Or more precisely, you can't give them new names instead of Item1 and Item2 defined in the BCL.
you can't give them new names instead of Item1 and Item2 defined in the BCL
But you can add System.Runtime.CompilerServices.TupleElementNamesAttribute to override names for user convenience: https://sharplab.io/#v2:D4AQTAjAsAUCDMACciDCiDetE+UgFAJYB2ALogMoD2AtgKakAWJA5gDSIBmANlQIbkA8kzoAnACrNiLAJSIAsvjlYYuNcgDsiACZ1OfAK7dSAbmy4AvrAtA=
Huh, interesting. In any case, the attribute is tuple-specific, which doesn't help us with naming type arguments into 3rd party types like ICollection<name: string>.
It's possible to add names when member is declared:
class TypeParameterNamesAttribute : Attribute
{
public TypeParameterNamesAttribute(object[] names) { }
}
[TypeParameterNames(new object[] { "versionId", new object[] { "docName", "docId" } })]
class SomeMapData : Dictionary<string, Dictionary<string, string>> { } // it's inheritance instead of alias