I would like to do something like this with the new Scala Dotty compiler:
trait Monad[M[A]] (underlyingValue:A) {
def bind[A, B](f: A => M[B]): M[B]
}
or at least
class Monad[M[A]] (underlyingValue:A) {
def bind[A, B](f: A => M[B]): M[B]
}
but the compiler complains that Not found: type A
is there someway to access the type parameter of a type parameter? Or something with the same end result but done differently?
I know that you can create a Monad like here: https://dotty.epfl.ch/docs/reference/contextual/type-classes.html but having a Monad class would allow me to declare a class a Monad in the same place it is defined, and would also make more sense to my stylistically.
Is there some way to do this?
For what it's worth, The following solution fulfills the criteria I had in mind:
trait Monad[F[_], A](underlyingValue: A) {
def flatMap[B](f: A => F[B]):F[B]
}
And usage would look like:
class Opt[A](underlyingValue: A) extends Monad[Opt, A](underlyingValue: A) {
def flatMap[B](f: A => Opt[B]):Opt[B] = {
...
}
}
While it does require two type parameters, the type parameter "A" is not repeated twice, and as such there's no logic duplication.