Skip to content
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

chooseType for List, Array and Seq modules #527

Closed
5 of 6 tasks
sergey-tihon opened this issue Dec 28, 2016 · 12 comments
Closed
5 of 6 tasks

chooseType for List, Array and Seq modules #527

sergey-tihon opened this issue Dec 28, 2016 · 12 comments

Comments

@sergey-tihon
Copy link

sergey-tihon commented Dec 28, 2016

Enumerable.OfType equivalent for List, Array and Seq modules

I propose to extend List, Array and Seq modules with equivalent to Enumerable.OfType

The existing way of approaching this problem in F# is

// Option 1
itemsArray |> Array.choose (function | :? 'a as x -> Some(x) | _ -> None)
// Option 2
itemsArray.OfType<TResult>() |> Seq.toArray

more options a mentioned on StackOverflow

Pros and Cons

The advantages of making this adjustment to F# are:

  • Improvements of code readability and succinctness
  • Avoidance of the LINQ usage.

The disadvantages of making this adjustment to F#

  • ???

Extra informtion

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

Related suggestions: (put links to related suggestions here)

Affidavit (must be submitted)

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 would be willing to help implement and/or test this
  • I or my company would be willing to help crowdfund F# Software Foundation members to work on this
@slang25
Copy link

slang25 commented Jan 1, 2017

I'd like to vote for ofType being defined in each module. At the moment in a lot of my projects I use this:

Module Seq = 
    let ofType<'a> (items: _ seq)= items |> Seq.choose(fun i -> match box i with | :? 'a as a -> Some a |_ -> None)

I think this makes it discoverable, and where people might expect to find it.

@ErikSchierboom
Copy link
Contributor

Nice suggestion! I assume this is a relatively small feature, so could this be something that someone new to the F# compiler (like me) can pick up? If so, I'd love to give it a go.

@Happypig375
Copy link
Contributor

Has this been approved yet?

@cartermp
Copy link
Member

cartermp commented Aug 6, 2019

@Happypig375 Nope, that requires the approved-in-principle label.

@dsyme
Copy link
Collaborator

dsyme commented Aug 16, 2019

@cartermp What do you think about this?

  • Historically we haven't particularly emphasized runtime-type-based classification/filtering/querying as a technique in FSharp.Core. But it's entirely consistent with the rest of F# to do so.

  • There are no downvotes, a good number of upvotes.

So I'd say yes, we could do this, there's no real reason not to

@cartermp
Copy link
Member

My only question would be if this is also considered valid for Fable usage, but otherwise I don't see a reason not to add this

@deviousasti
Copy link

Fable can test the run-time type limited to classes.

let isOfT = k :? T
export const isOfT = k instanceof T;

@Liminiens
Copy link

Liminiens commented Nov 10, 2020

It’s reasonable to have such functions at core library when you do interop with frameworks like Xamarin, where you sometimes have to get a collection of a more concrete type. Or find first element that satisfies some type. Example is Effects on a Xamarin.Forms control.

@Happypig375
Copy link
Contributor

Happypig375 commented May 30, 2021

@Happypig375
Copy link
Contributor

This should be called chooseType instead of ofType because functions starting with of in F# signify creation from another type which is not the case here, instead this function is more similar to choose.

@Happypig375
Copy link
Contributor

Happypig375 commented May 31, 2021

@dsyme dsyme changed the title Enumerable.OfType<TResult> equivalent for List, Array and Seq modules chooseType for List, Array and Seq modules Jun 9, 2022
@dsyme
Copy link
Collaborator

dsyme commented Jan 9, 2023

The RFC has been archived as we decided not to progress it. I will close this suggestion as declined

@dsyme dsyme closed this as completed Jan 9, 2023
@abelbraaksma abelbraaksma closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants