首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有同伴的顶级类只能扩展为同名类或由同名同伴组成的块。

没有同伴的顶级类只能扩展为同名类或由同名同伴组成的块。
EN

Stack Overflow用户
提问于 2020-06-20 18:22:14
回答 1查看 484关注 0票数 2

我试图按以下方式使用https://github.com/estatico/scala-newtype

代码语言:javascript
复制
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)

}

编译器抱怨:

代码语言:javascript
复制
[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的内容

代码语言:javascript
复制
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对象中,如下所示:

代码语言:javascript
复制
package object db {
  
  @newtype case class DbUrl(v: String)

  @newtype case class DbUser(v: String)

  @newtype case class DbPw(v: String)

}

但是编译器仍然抱怨:

代码语言:javascript
复制
implicit conversion method opsThis should be enabled
[error] by making the implicit value scala.language.implicitConversions visible.
[error]   @newtype case class DbUser(v: String)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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的定义类似于这样(我只是从自述文件中复制它):

代码语言:javascript
复制
type Base = Any { type DbUrl$newtype }
trait Tag extends Any
type Type <: Base with Tag
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62489970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档