Scala &引用;常数;case类中的值

Scala &引用;常数;case类中的值,scala,constants,case-class,Scala,Constants,Case Class,考虑“常量”值:DefaultEncoding。通常,我们会将其添加到伴生对象: object Strlen { val DefaultEncoding = "ISO-8859-1" .. } 但是,我们不得不避免使用伴随对象,因为Strlen必须是一个case类,以对应于重要的现有代码库中的代码结构/约定: case class Strlen(child: Expression, encoding : Expression) extends UnaryExpression with

考虑“常量”值:DefaultEncoding。通常,我们会将其添加到伴生对象:

object Strlen {
  val DefaultEncoding = "ISO-8859-1"
  ..
}
但是,我们不得不避免使用伴随对象,因为Strlen必须是一个case类,以对应于重要的现有代码库中的代码结构/约定:

case class Strlen(child: Expression, encoding : Expression) extends UnaryExpression with LengthExpression {

  val DefaultEncoding = "ISO-8859-1"    // This will not be isible in the following constructor

  def this(child: Expression) = this(child, new Literal(DefaultEncoding, StringType))
那么,有没有办法在case类中实现DefaultEncoding“constant”的划分呢

更新根据wingedsubmariner的建议,我在case类中尝试了以下方法:

def DefaultEncoding=“ISO-8859-1”

但是它不编译

[info] Compiling 1 Scala source to /shared/spark-master/sql/catalyst/target/scala-2.10/classes...
[error] /shared/spark-master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala:253: not found: value DefaultEncoding
[error]   def this(child: Expression) = this(child, new Literal(/* StrConstants. */DefaultEncoding, StringType))
[error]       

您可以将伴生对象与案例类一起使用。如果在同一个编译单元(通常是同一个文件)中声明一个具有相同名称的对象,则该对象将被视为案例类的伴生对象,并将常规案例类伴生对象方法(例如,
unapply
)添加到该对象中


另一个选项是将
DefaultEncoding
声明为
def
。因为字段访问总是通过Scala中的访问器完成的,所以不会有任何性能损失。

案例类编写自定义伴生对象不会阻止编译器为案例类提供默认的帮助器方法

trait Expression
trait UnaryExpression extends Expression
trait LengthExpression extends Expression

trait Typ
case object StringType extends Typ

case class Literal(val encoding: String, val typ: Typ) extends Expression

case class StrLen(child: Expression, encoding: Expression) extends UnaryExpression with LengthExpression {
  def this(child: Expression) = this(child, new Literal(StrLen.DefaultEncoding, StringType))
}

object StrLen {
  val DefaultEncoding = "ISO-8859-1"
  def apply(child: Expression): StrLen = apply(child, new Literal(StrLen.DefaultEncoding, StringType))
}

case object ExampleExpression extends Expression

println(StrLen(ExampleExpression))
// --> StrLen(ExampleExpression,Literal(ISO-8859-1,StringType))
println(new StrLen(ExampleExpression))
// --> StrLen(ExampleExpression,Literal(ISO-8859-1,StringType))
def isProduct[T <: Product] {}
isProduct[StrLen]
特质表达
trait-UnaryExpression扩展了表达式
性状长度表达扩展了表达
性状类型
案例对象StringType扩展类型
case类Literal(val编码:String,val类型:typ)扩展了表达式
case类StrLen(child:Expression,encoding:Expression)用lengtheexpression扩展了UnaryExpression{
def this(child:Expression)=this(child,新文本(StrLen.DefaultEncoding,StringType))
}
对象StrLen{
val DefaultEncoding=“ISO-8859-1”
def apply(子:表达式):StrLen=apply(子,新文本(StrLen.DefaultEncoding,StringType))
}
case对象ExampleExpression扩展表达式
println(StrLen(示例表达式))
//-->StrLen(示例表达式,文本(ISO-8859-1,StringType))
println(新StrLen(示例表达式))
//-->StrLen(示例表达式,文本(ISO-8859-1,StringType))

def iProduct[T我需要Case类默认伴生对象附带的产品接口实现-因此您的第一个建议不太理想。我不想自己将其作为一般规则进行编码。我现在正在尝试def建议。根据@Utaal,产品接口无论如何都可以工作。谢谢,我今天上午很紧张,但会合作让我回来验证。同时进行投票。事实上,如果你声明一个自定义的伴星对象,编译器不会添加一些方法,例如,
tupled
不存在。看到这个问题了吗谢谢,我今天早上很忙,但会回来验证。同时进行投票。实际上,编写自定义伴星对象确实会删除一些数据efault helper方法,编译器将不再添加这些方法。请参阅此问题:case类中不能有常量,命名应该是小写
defaultEncoding
@samthebest是的,我知道这两种方法-这就是此问题的重点,询问如何处理此问题。请指出如何解决此问题相反