New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a seq.toIList extension #785
Comments
Should be called |
This can also be said for |
F# lists also satisfy the |
|
@Happypig375 This doesn't change my statement. |
Yes, I found out about ResizeArray when I asked a friend on Twitter. I just think it's quite non-obvious as the solution to this problem. When the API requires something that satisfies IList, it's not immediately apparent that ResizeArray is the answer. And neither the compiler nor the intellisense are much help in figuring out the answer. My perspective is as someone who uses F# without having had a grounding in the wider .NET ecosystem, so I don't have an intuitive understanding of the different interfaces (which is my failing, to an extent). There's already a slight eyebrow raise when you first learn that an F# list is not the same as a .NET list, but when the available methods to convert between them when you need to are non-obvious, or hidden in System.Linq, or whatever, it doesn't help on the usability front. And you need to know a little bit of background in order to make a sensible search on StackOverflow. |
Is actually what is really required then just an alis for the |
If System.Collections.Generic.List is wanted you can use constructor of it directly. No ResizeArray alias type is need to know nor C#ish System.Linq .ToList() is required. And if IList is wanted you can currently use seq.toArray too. If you don't have C# background and you are using C# API with F# it can have it weirdness (almost by definition) but I think this won't improve things much. I think that default flow of thought should be something like: interface is wanted but type you have is not implementing that. Find implementation candidate and see how it can be constructed. Instead of looking your original source object/module for .To# methods/functions. With all sympathy. |
I think it has to focus for what different "list" means between C# and F#. For example, it's made for an operator named "Seq.toMutableList" with result for System.Collections.Generic.List<'a>. |
@kekyo this is exactly what |
I think @isaacabraham 's suggestion as above about creating an alias would probably be fine. That would be enough to make obvious (or with a reasonable guess) what method to use to achieve the goal without having to head straight to StackOverflow to find out. |
@isaacabraham Exactly yes! module Seq =
let toMutableList (e: _ seq) = System.Linq.Enumerable.ToList(e)
let source = [123; 456; 789]
let list = source |> Seq.toList
let ra = new ResizeArray<_>(source)
let ml = source |> Seq.toMutableList We can tell them how to switch immutable and mutable only Seq function naming when it declared. I feel better integrity for using. (And I understand bit not good, it'll declare more usage aliasing...) |
Closing per discussion above |
I propose we add a simple extension to the library to convert from a collection to an IList, to make life easier when using certain APIs
It could implement something like:
used like, for example in a .NET Google API:
where "Repeatable" expects a Collections.Generic.IEnumerable.
The existing way of approaching this problem in F# is to use a method like ResizeArray or to open System.Linq and System.Collections.Generic in the module, which seems like overkill.
Pros and Cons
The advantages of making this adjustment to F# are:
a) It's useful for the above case of working with existing .NET APIs, and probably for other things too.
b) It would help people who are new to F# (whether from a .NET background or not) make sense of the difference between a .NET List and an F# list.
c) Using ResizeArray for this purpose seems a bit hack-y and a random bit of insider knowledge. Nothing about it screams "use this when converting from list to List".
d) It's really simple - all of the required code is above.
The disadvantages of making this adjustment to F# are:
a) Is it a slippery slope? Will there end up being a proliferation of these methods?
Extra information
Estimated cost (XS, S, M, L, XL, XXL):
XS
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: