I am using Scala 2.13, and I am developing the Reader monad my own. The implementation of the monad is the following.
object ReaderMonad {
implicit def reader[From, To](f: From => To): Reader[From, To] = Reader(f)
case class Reader[From, To](f: From => To) {
def run(input: From): To =
f(input)
def map[NewTo](transformation: To => NewTo): Reader[From, NewTo] =
Reader(c => transformation(f(c)))
def flatMap[NewTo](transformation: To => Reader[From, NewTo]): Reader[From, NewTo] =
Reader(c => transformation(f(c)).run(c))
}
def pure[From, To](a: To): Reader[From, To] = Reader((c: From) => a)
}
Using such a monad, I am defining a repository for stocks.
trait StockRepository {
def findAll: Map[String, Double]
}
Stocks service uses the implementation of the repository, using the Reader monad to inject the repo dependency.
object Stocks {
def findAll: Reader[StockRepository, Map[String, Double]] =
(repo: StockRepository) => repo.findAll()
}
My question is, why have I to specify the repo parameter type explicitly in the last function definition, (repo: StockRepository) => repo.findAll()? Why cannot the Scala compiler infer the type implicitly for me?
Thanks a lot.
Sure, you only need to remove the implicit conversion and add the
apply.However, I added some other changes to make the code more idiomatic and easier to use, if you have any doubt just let me know.
And you can use it like this: