如何轻松获取 Scala 案例类的名称?

发布于 2024-08-29 12:56:38 字数 312 浏览 7 评论 0原文

给定:

case class FirstCC {
  def name: String = ... // something that will give "FirstCC"
}
case class SecondCC extends FirstCC
val one = FirstCC()
val two = SecondCC()

如何从 one.name 获取 "FirstCC" 和从 two.name 获取 "SecondCC"

Given:

case class FirstCC {
  def name: String = ... // something that will give "FirstCC"
}
case class SecondCC extends FirstCC
val one = FirstCC()
val two = SecondCC()

How can I get "FirstCC" from one.name and "SecondCC" from two.name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心如狂蝶 2024-09-05 12:56:38
def name = this.getClass.getName

或者,如果您只想要不带包的名称:

def name = this.getClass.getSimpleName

请参阅 java.lang.Class 了解更多信息。

def name = this.getClass.getName

Or if you want only the name without the package:

def name = this.getClass.getSimpleName

See the documentation of java.lang.Class for more information.

开始看清了 2024-09-05 12:56:38

您可以使用案例类的属性productPrefix

case class FirstCC {
  def name = productPrefix
}
case class SecondCC extends FirstCC
val one = FirstCC()
val two = SecondCC()

one.name
two.name

NB
如果你传递到 scala 2.8 扩展一个已被弃用的案例类,你必须不要忘记左右父 ()

You can use the property productPrefix of the case class:

case class FirstCC {
  def name = productPrefix
}
case class SecondCC extends FirstCC
val one = FirstCC()
val two = SecondCC()

one.name
two.name

N.B.
If you pass to scala 2.8 extending a case class have been deprecated, and you have to not forget the left and right parent ()

对不⑦ 2024-09-05 12:56:38
class Example {
  private def className[A](a: A)(implicit m: Manifest[A]) = m.toString
  override def toString = className(this)
}
class Example {
  private def className[A](a: A)(implicit m: Manifest[A]) = m.toString
  override def toString = className(this)
}
£冰雨忧蓝° 2024-09-05 12:56:38
def name = this.getClass.getName
def name = this.getClass.getName
浅浅 2024-09-05 12:56:38

下面是一个 Scala 函数,它可以从任何类型生成人类可读的字符串,并在类型参数上递归:

https:// gist.github.com/erikerlandson/78d8c33419055b98d701

import scala.reflect.runtime.universe._

object TypeString {

  // return a human-readable type string for type argument 'T'
  // typeString[Int] returns "Int"
  def typeString[T :TypeTag]: String = {
    def work(t: Type): String = {
      t match { case TypeRef(pre, sym, args) =>
        val ss = sym.toString.stripPrefix("trait ").stripPrefix("class ").stripPrefix("type ")
        val as = args.map(work)
        if (ss.startsWith("Function")) {
          val arity = args.length - 1
          "(" + (as.take(arity).mkString(",")) + ")" + "=>" + as.drop(arity).head
        } else {
          if (args.length <= 0) ss else (ss + "[" + as.mkString(",") + "]")
        }
      }
    }
    work(typeOf[T])
  }

  // get the type string of an argument:
  // typeString(2) returns "Int"
  def typeString[T :TypeTag](x: T): String = typeString[T]
}

Here is a Scala function that generates a human-readable string from any type, recursing on type parameters:

https://gist.github.com/erikerlandson/78d8c33419055b98d701

import scala.reflect.runtime.universe._

object TypeString {

  // return a human-readable type string for type argument 'T'
  // typeString[Int] returns "Int"
  def typeString[T :TypeTag]: String = {
    def work(t: Type): String = {
      t match { case TypeRef(pre, sym, args) =>
        val ss = sym.toString.stripPrefix("trait ").stripPrefix("class ").stripPrefix("type ")
        val as = args.map(work)
        if (ss.startsWith("Function")) {
          val arity = args.length - 1
          "(" + (as.take(arity).mkString(",")) + ")" + "=>" + as.drop(arity).head
        } else {
          if (args.length <= 0) ss else (ss + "[" + as.mkString(",") + "]")
        }
      }
    }
    work(typeOf[T])
  }

  // get the type string of an argument:
  // typeString(2) returns "Int"
  def typeString[T :TypeTag](x: T): String = typeString[T]
}
满身野味 2024-09-05 12:56:38
def name = getClass.getSimpleName.split('

这将删除某些类末尾出现的 $1

).head

这将删除某些类末尾出现的 $1

def name = getClass.getSimpleName.split('

This will remove the $1 appearing at the end on some classes.

).head

This will remove the $1 appearing at the end on some classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文