简体   繁体   中英

Scala: How to get attribute names of a case class

I am new to Scala and I have a case class "Lr". I need to print all the attribute names as header delimited by tab like:

some1_ID    some2_ID

But I am getting ans:

value some1_ID  value some2_ID

May I get help on how to modify my code to get the right ans?

package com.......

case class Lr (
  some1_ID : Option[String],
  some2_ID : Option[String]
)

object EchoLr {

  def classAccessors[T: TypeTag]: String = typeOf[T].members.collect { 
    case m: MethodSymbol if m.isCaseAccessor => m
  }.mkString("\t")

  def main( args:Array[String] ):Unit = {
    val testLR = Lr(Option("something1"),Option("something2"))
    println(classAccessors[Lr])
  }
}

I was following :

Get field names list from case class

Replace

case m: MethodSymbol if m.isCaseAccessor => m

by

case m: MethodSymbol if m.isCaseAccessor => m.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM