Re: Type errors with polymorphic higher order type
To compile BasicRunner correct your monadOps must be of type Monad[Id]
where Id is a type constructor.
Are you sure that value Id is of type Monad[Id] ?
On Jul 1, 6:48 am, Mitchell Hashimoto <mitchell.hashim...@...>
wrote:
> Hello,
>
> I've created the following basic trait for monads in Scala:
>
> trait Monad[M[_]] {
> def unitM[A](a: A): M[A]
> def bindM[A, B](a: M[A], f: A => M[B]): M[B]
>
> }
>
> And I've created the identity monad successfully. However, when I try to
> make a higher order abstraction, so that I can easily switch in new monads
> for my computations, then I get type errors. Example:
>
> trait Runner {
> type M[_]
> val monadOps: Monad[M]
>
> def run {
> val one = monadOps.unitM(1)
> val result = monadOps.bindM[Int, Int](one, x => monadOps.unitM(x + 2))
> println(monadOps.showM(result))
> }
>
> }
>
> object BasicRunner extends Runner {
> type M[_] = Id[_]
> val monadOps = Id
>
> }
>
> BasicRunner.run
>
> Without the higher orderness, this works fine. But once I introduce the
> abstract members M and monadOps, I get the following error:
>
> error: overriding value monadOps in trait Runner of type
> this.Monad[Main.$anon.BasicRunner.M];
> value monadOps has incompatible type
> val monadOps = Id
>
> Any hints?
>
> Thanks,
> Mitchell