Skip to content

Type constraints for type class instances? #1887

Answered by gcanti
willardthor asked this question in Q&A
Discussion options

You must be logged in to vote

Is there a way to make Tagged an instance of, say, Functor, whilst keeping that type constraint on T?

Unfortunately no, there's a workaround when you can fix the second type parameter (see for example getApplicativeValidation in the Either module), i.e.

const getFunctor = <T extends Tag>(): Functor2C<URI, T> => ({
  URI,
  _E: undefined as any,
  map
})

const functor = getFunctor<"Num">()

const v = tag("Num", of(5)) // Tagged<"Num", number>
const a = map(v, (x: number) => x + 1) // Tagged<"Num", number>
const b = functor.map(v, (x: number) => x + 1) // Tagged<"Num", number>
const c = functor.map([42, 5], (x: number) => x + 1) // error

but is admittedly ugly

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by willardthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants