0

I have the following code that does not compiled:

trait DbSetup[F[_]] {

  type EnvT[A] = OptionT[F, A]

  def system: EnvT[Env]

  def user: EnvT[String]

  def password: EnvT[String]

  def address: EnvT[String]

}

object DbSetup {

  def read[F[_]: Monad](s: DbSetup[F]): EnvT[Configuration] = ???

}

the compiler complains:

not found: type EnvT
[error]   def read[F[_]: Monad](s: DbSetup[F]): EnvT[Configuration] = ???
[error]                                         ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed 

How to import the type EnvT into the object scope?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

4

Try

def read[F[_]: Monad](s: DbSetup[F]): s.EnvT[Configuration] = ???
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • 3
    @zero_coding Google for "path-dependent type", "method dependent type" https://stackoverflow.com/questions/54879353/how-to-write-method-with-dependent-type-in-scala Have you looked at the link I gave https://stackoverflow.com/questions/54879353/how-to-write-method-with-dependent-type-in-scala ? – Dmytro Mitin Jun 05 '20 at 20:40
  • 2
    @zero_coding Every instance of `DbSetup` kinda has its own version of `EnvT`. By doing `s.EnvT`, you're accessing `s`'s specific copy of that type – user Jun 05 '20 at 20:40
  • @zero_coding Could you be more specific what confuses you? – Dmytro Mitin Jun 05 '20 at 20:41
  • 1
    @zero_coding https://stackoverflow.com/questions/2693067/what-is-meant-by-scalas-path-dependent-types – Dmytro Mitin Jun 05 '20 at 20:43
  • I was confused, how do you know that can be written `s.EnvT[Configuration]` – softshipper Jun 05 '20 at 20:46
  • 3
    @zero_coding well, I couldn't know that it was specifically `s.EnvT`. It could be any `x.EnvT` for `x: DbSetup[F]`. For example for `val x = new DbSetup[F] { override ...}`. You could make `import x._` so that you could access `x.EnvT` as just `EnvT`. But there was already `x: DbSetup[F]` in the scope, namely `s`. – Dmytro Mitin Jun 05 '20 at 20:54