0

I have the following code, that does not compile:

import cats._
import cats.implicits._
import cats.data._
import cats.syntax.applicative._      // for pure
import cats.syntax.applicativeError._ // for raiseError etc


final class DbSystemEnvironment[F[_]: MonadError[*[_], Throwable]] private(env: Environment[F])
  extends DbSetting[F] {
  override def read(url: String, user: String, pw: String): F[DbParameter] =
    (for {
      a <- OptionT(env.get(url))
      b <- OptionT(env.get(user))
      c <- OptionT(env.get(pw))
    } yield DbParameter(a, b, c))
      .value
      .flatMap {
        case Some(v) => v.pure[F]
        case None => DbSettingError.raiseError[F, DbParameter]
      }

}

the compiler complains:

[error] /home/developer/scala/user-svc/src/main/scala/io/databaker/db/DbSystemEnvironment.scala:20:27: value pure is not a member of io.databaker.db.DbParameter
[error] did you mean url?
[error]         case Some(v) => v.pure[F]
[error]                           ^
[error] /home/developer/scala/user-svc/src/main/scala/io/databaker/db/DbSystemEnvironment.scala:21:37: value raiseError is not a member of object io.databaker.db.DbSettingError
[error]         case None => DbSettingError.raiseError[F, DbParameter]
[error]                                     ^
[error] two errors found

What am I missing?

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • 2
    If you already import all implicits, do not import specific syntax. That would introduce ambiguities and make your code fail. – Luis Miguel Mejía Suárez Jun 18 '20 at 14:10
  • which implicit is wrong? – softshipper Jun 18 '20 at 14:16
  • 2
    If you are using just `import cats.implicits._` that is it, do not import the two extra `cats.syntax` – Luis Miguel Mejía Suárez Jun 18 '20 at 14:18
  • Could you please tell me, how do you know that. Where to get such as information? – softshipper Jun 18 '20 at 14:33
  • 3
    If you know how implicits work, you know why there cannot be two implicits of the same type on the same level of "closeness" to the call site. `cats.implicits._` improrts all implicits in cast while `cats.syntax.nameofsyntax._` some of them - logically, some of implicits would be duplicated (which of them, can be checked by looking to code or summoning them and observing errors), so functionalities which would rely on them, will fail with ambiguous implicits. – Mateusz Kubuszok Jun 18 '20 at 14:46
  • @zero_coding *Could you please tell me, how do you know that.* You can find this information in tutorials I recommended you [here](https://stackoverflow.com/questions/62224033/use-mapn-to-apply-values). Namely e.g. here http://eed3si9n.com/herding-cats/import-guide.html Also read advice 2 here https://blog.softwaremill.com/9-tips-about-using-cats-in-scala-you-might-want-to-know-e1bafd365f88 And please next time prepare [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). Don't make people go to your previous questions for definitions of classes and methods you used. – Dmytro Mitin Jun 19 '20 at 16:40
  • chapters 1.4.1 Importing Type Classes, 1.4.2 Importing Default Instances, 1.4.3 Importing Interface Syntax, 1.4.4 Importing All The Things! in "Scala with Cats" https://www.scalawithcats.com/dist/scala-with-cats.pdf – Dmytro Mitin Jun 19 '20 at 16:47
  • https://typelevel.org/cats/typeclasses/imports.html – Dmytro Mitin Jun 19 '20 at 16:49

0 Answers0