Are contraints on function parameters possible? And how do I call the interfaces/functions of type classes? #1629
Answered
by
xuanduc987
philschonholzer
asked this question in
Q&A
-
Is it possible to write the following Haskell code with fp-ts? I want my function to accept any arguments that implement the Monoid type class. merge :: Monoid m => m -> m -> m
merge m1 m2 = m1 <> m2 How would I would this be written in TS? const merge = (m1: ???, m2: ???): ??? => {
return ???.concat(m1, m2);
} The constraint is one thing, but how do I get my Is this even possible? And how? |
Beta Was this translation helpful? Give feedback.
Answered by
xuanduc987
Dec 3, 2021
Replies: 1 comment 1 reply
-
Yes, it is possible but you will have to pass typeclass instance around. import { Monoid } from 'fp-ts/Monoid'
import { string } from 'fp-ts'
const merge = <A>(M: Monoid<A>) => (m1: A, m2: A): A => {
return M.concat(m1, m2)
}
console.log(merge(string.Monoid)("abc", "xyz")) // => abcxyz |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
philschonholzer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it is possible but you will have to pass typeclass instance around.