That probably means a

Monoid[A] needs to exist. You can encode that dependency with an implicit parameter or context bound. Assuming you encode typeclass instances with implicits.

For instance:

implicit def optionMonoid[A](implicit monoidA: Monoid[A]): Monoid[Option[A]] = new Monoid[Option[A]] {

def combine(a1: Option[A], a2: Option[A]) = a1.flatMap(aa1 => a2.map(aa2 => monoidA.combine(aa1, aa2)))

def identity = Option(monoidA.identity)

}

Cats implements it a bit differently: https://github.com/typelevel/cats/blob/v0.9.0/kernel/src/main/scala/cats/kernel/instances/option.scala#L61

They only require a

Semigroup[A].Op donderdag 20 april 2017 12:29:34 UTC+2 schreef Joe San:

Let's say that I have a Monoid trait as below:

```
trait Monoid[A] {
def combine(a1: A, a2: A): A
def identity: A
}
```

Now if I want to write an optionMonoid for this, I could write it like this:

```
val optionMonoid1 = new Monoid[Option[A]] {
def combine(a1: Option[A], a2: Option[A2]) a1 orElse a2
def identity = None
}
```

This given the fact that I do not know anything about the inner type in the Option. But what if I want to have the combine operator in such a way that I want to really combine the inner types in the Option?

--

You received this message because you are subscribed to the Google Groups "scala-user" group.

To unsubscribe from this group and stop receiving emails from it, send an email to

[hidden email].

For more options, visit

https://groups.google.com/d/optout.