我试图按以下方式使用https://github.com/estatico/scala-newtype:
import io.estatico.newtype.macros.newtype
import cats._
import io.databaker.env._
@newtype case class DbUrl(v: String)
@newtype case class DbUser(v: String)
@newtype case class DbPw(v: String)
final case class DbParams(url: DbUrl, user: DbUser, pw: DbPw)
trait DbConnector[F[_]] {
def read(url: DbUrl, user: DbUser, pw: DbPw): F[DbParams]
}
object DbConnector {
def impl[F[_] : MonadError[*[_], Throwable]](env: Environment[F])
: DbConnector[F] =
new LiveDbConnector[F](env)
}编译器抱怨:
[error] ../db/DbConnector.scala:7:2: top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
[error] @newtype case class DbUrl(v: String)
[error] ^
[error] ../db/DbConnector.scala:9:2: top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
[error] @newtype case class DbUser(v: String)
[error] ^
[error] ../db/DbConnector.scala:11:2: top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
[error] @newtype case class DbPw(v: String)
[error] ^
[error] ../env/Environment.scala:8:2: top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
[error] @newtype case class EnvValue(v: String)
[error] ^
[error] ../env/Environment.scala:6:2: top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
[error] @newtype case class EnvVariable(v: String) build.sbt的内容
lazy val root = (project in file("."))
.enablePlugins(JettyPlugin)
.settings(
organization := "io.example",
name := "user-svc",
version := "0.0.1-SNAPSHOT",
scalaVersion := "2.13.2",
mainClass := Some("io.example.Main"),
containerPort := 9090,
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-servlet" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"org.http4s" %% "http4s-dsl" % Http4sVersion,
"io.circe" %% "circe-generic" % CirceVersion,
"org.scalameta" %% "munit" % MunitVersion % "test",
"ch.qos.logback" % "logback-classic" % LogbackVersion,
"io.estatico" %% "newtype" % NewTypeVersion,
"javax.servlet" % "javax.servlet-api" % ServletVersion % "provided"
),
addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.11.0" cross CrossVersion.full),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"),
)
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8",
"-language:higherKinds",
"-language:postfixOps",
"-feature",
"-Xfatal-warnings",
"-Ymacro-annotations"
)我做错了什么?
更新
我已经将新类型的宏移动到package对象中,如下所示:
package object db {
@newtype case class DbUrl(v: String)
@newtype case class DbUser(v: String)
@newtype case class DbPw(v: String)
}但是编译器仍然抱怨:
implicit conversion method opsThis should be enabled
[error] by making the implicit value scala.language.implicitConversions visible.
[error] @newtype case class DbUser(v: String)发布于 2020-06-20 18:36:24
scala的README.md说:
这将扩展为类型和伴生对象定义,因此必须在对象或包对象中定义新类型。
宏可以将类扩展到具有相同名称和伴生对象的其他类中,但据我所知,newtype注释将您的case class转换为同名的对象(以及类似type DbUrl = DbUrl.Type的类型别名)。这种行为(将顶级注释者转换为其他类型的树)是不允许的。但是,如果注释生成了一个类DbUrl,或者是一个同名的对象,那么它就没有问题,但是其他的东西几乎都不能工作。
要解决您的问题,只需将其移动到包对象(或其他范围,只要它不是顶级的)。
编辑:正如Dmytro所指出的,创建的类型不是DbUrl类型,而是类似于大写"T“的type DbUrl = DbUrl.Type,其中DbUrl.Type的定义类似于这样(我只是从自述文件中复制它):
type Base = Any { type DbUrl$newtype }
trait Tag extends Any
type Type <: Base with Taghttps://stackoverflow.com/questions/62489970
复制相似问题