fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
345 stars 21 forks source link

Type abbreviations for types specially supported by F# #980

Closed Happypig375 closed 2 years ago

Happypig375 commented 3 years ago

Type abbreviations for types specially supported by F

I propose we add:

type KeyValue<'Key, 'Value> = System.Collections.Generic.KeyValuePair<'Key, 'Value> // Mirrors (|KeyValue|)
type dict<'Key, 'Value> = System.Collections.Generic.IDictionary<'Key, 'Value> // Mirrors the dict function
type readOnlyDict<'Key, 'Value> = System.Collections.Generic.IReadOnlyDictionary<'Key, 'Value> // Mirrors the readOnlyDict function
type task<'T> = System.Threading.Tasks.Task<'T> // Mirrors the upcoming task computation expression
type vtask<'T> = System.Threading.Tasks.ValueTask<'T> // Mirrors voption where struct versions also receive an abbreviation
type taskSeq<'T> = System.Collections.Generic.IAsyncEnumerable<'T> // Mirrors the taskSeq computation expression mentioned in https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1087-resumable-code-and-task-builder.md#example-taskseq---

The existing way of approaching this problem in F# is to refer to them by their long names while wondering why e.g. type abbreviations like exn and Lazy<'T> exist but not the above that are also specially supported.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Consistency
  2. Convenience
  3. Conciseness

The disadvantages of making this adjustment to F# are none.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): XS

Related suggestions: (None)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

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.

charlesroddie commented 3 years ago

These alternate names come with a cost. To use them you need to know the F# names, and then at some point (reading .Net documentation, using non-language-specific .Net libraries, interop...) will need to learn the true names of the types, and either remember or else repeatedly look up the correspondence. I think they should only be used when there is an exceptionally large benefit that outweighs the cost. The name block for ImmutableArray is an example where the cost could be justified, where the type is likely to become ubiquitous and the name being replaced is much longer.

Happypig375 commented 3 years ago

@charlesroddie But those names either already exist or will be added to FSharp.Core.

cartermp commented 3 years ago

The broader point here is if we intentionally want to add more and more type abbreviations to F# for the sake of a more F#-friendly name or not. This obviously has a disadvantage because it requires you to understand the underlying type and that there's a mapping to it in the F# world. For things like int and string it's obvious enough because they're used everywhere, but the more specialized the construct the less easy that is. I think this is simple enough for .NET experts to manage, but I don't have a sense of how well a newcomer would handle several different kinds of type abbreviations.

Another point is that precedent matters, but just because something was done before doesn't mean it was a good idea. I could see a perfectly valid argument for type abbreviations beyond the primitives being a net-negative.

Lastly I do not think we should preemptively add an abbreviation for something in anticipation of "proper support" for that thing like task and so on.

jwosty commented 3 years ago

Some of these suggestions make a lot of sense because there's already precedent for the proposed name, where it's already somewhere else in the language, be it function names or active pattern names; namely, KeyValue, dict, readOnlyDict. I think these have a stronger case than the other three (though I myself am not opposed to all of them).

dsyme commented 2 years ago

Closing for the reasons described by @cartermp here: https://github.com/fsharp/fslang-suggestions/issues/980#issuecomment-779356059