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

How to implement filter for custom type? #216

Closed
cmeeren opened this issue Dec 19, 2019 · 2 comments
Closed

How to implement filter for custom type? #216

cmeeren opened this issue Dec 19, 2019 · 2 comments

Comments

@cmeeren
Copy link
Contributor

cmeeren commented Dec 19, 2019

I have a custom type Skippable<'a> that's just like Option (but semantically different). It's defined in a separate library.

In my code, I want to extend this type to support filter. With that in mind, I have tried the following, but none of them work (I have also tried switching the arguments):

type Skippable<'a> with
  static member Filter(s, p) = Skippable.filter p s
type Skippable<'a> with
  member this.Filter(p) = Skippable.filter p this
type FSharpPlus.Control.Filter with
  static member Filter(s, p) = Skippable.filter p s

In all cases, when I try to use filter on a Skippable<_> value, I get a compiler error saying

No overloads match for method 'ToSeq'.

Is this because I am using extension members? Is it possible to add support to custom types using extension members?

@cannorin
Copy link
Member

cannorin commented Dec 19, 2019

F#+ currently does not support orphan instances i.e. does not allow type class instantiation if neither

  1. the instance is defined along with the type class itself (e.g. built-in with F#+), nor
  2. the instance is defined along with the target type (in your case, inside the FSharp.JsonSkippable)

This is a restriction coming from the F# compiler. However, dotnet/fsharp#6805 would enable us to define orphan instances.

@cmeeren
Copy link
Contributor Author

cmeeren commented Dec 19, 2019

I see, thanks for the clarification.

@cmeeren cmeeren closed this as completed Dec 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants