I am trying to use the first time tagless final pattern in Scala and struggling a bit.
I have the following algebras definition:
trait DbSetting[F[_]] {
def read(url: String, user: String, pw: String): F[DbParameter]
}
trait Environment[F[_]] {
def get(v: String) : F[Option[String]]
}
and the interpreters implementation:
final case class DbParameter(url: String, user: String, pw: String)
sealed trait DbError extends NoStackTrace
case object DbSettingError extends DbError
sealed trait DbError extends NoStackTrace
case object DbSettingError extends DbError
// Environment implementation
object Environment {
def apply[F[_]](implicit ev: Environment[F]): ev.type = ev
def impl[F[_]: Sync]: Environment[F] = new Environment[F] {
override def get(v: String): F[Option[String]] =
Sync[F].delay(sys.env.get(v))
}
}
// DbSetting implementation
class DbSystemEnvironment[F[_] : MonadError[*, Throwable]] private(env: Environment[F])
extends DbSetting[F] {
override def read(url: String, user: String, pw: String): F[DbParameter] = env.get(url)
}
What I am trying to do is, to compose Environment
into DbSystemEnvironment
. The problem here is, I can not get the value out of env.get(url)
because, I do not know anything about F
in DbSystemEnvironment
, except it is a MonadError
.
How to get the value out of env.get(url)::F[Option[String]]
?
In addition, if env.get(url)
returns Nothing
in read
function, then it should return MonadError
.
env.get(url)
"? You want to.map
it,.handle
errors or something else? Because with algebras you have defined forF
(Sync/MonadError), you can lift value, map/flatMap it handle, mapN, etc, but not turn F[A] into A (that would require Comonad). – Mateusz Kubuszok