Scala案例类

Scala案例类,scala,case-class,Scala,Case Class,下面是代码片段: package caseClassExp sealed trait Resources { def fullpath :String } case class Folder (name :String , path :Option[String] = None) extends Resources { def fullpath :String = path match { case Some(p) => List(p, name).m

下面是代码片段:

package caseClassExp

sealed trait Resources  {
  def fullpath :String
}

case class Folder (name :String ,
    path :Option[String] = None) extends Resources {
    def fullpath :String = path match {
      case Some(p) => List(p, name).mkString("/")
      case None => s"./$name"
    }
}

case class File (name :String ,
      folder :Option[Folder] = None ) extends Resources {
  def fullpath :String = folder match {
    case Some(f) => List(f.fullpath, name).mkString("/")
    case None => s"./$name"    
  }
}

object caseClass {
  def main(agrs:Array[String]):Unit = {
 
val resources = Seq[Resources] (
File("ex1.Scala",Some(Folder("example",Some("~/Dev"))))
Folder("temp")
Folder("bin",Some("/usr"))
File(".Clouder")
)  

resources foreach {
  case f :File => println(s"File: ${f.fullpath}")
  case f:Folder  => println(s"FOlder : ${f.fullpath}")
 }
}
}

当我主要调用File and Folder方法(定义为case类)时,我得到了一个错误,我不确定我做错了什么。

所以您的顺序不正确

您应该在序列中的每个元素后面加一个逗号

val resources = Seq[Resources] (
  File("ex1.Scala",Some(Folder("example",Some("~/Dev")))),
  Folder("temp"),
  Folder("bin",Some("/usr")),
  File(".Clouder")
)
结果是:

File: ~/Dev/example/ex1.Scala
FOlder : ./temp
FOlder : /usr/bin
File: ./.Clouder
所以一切都好:)除了逗号

在您的代码中,我的建议是将类型添加到资源中,并仅使用
Seq
创建序列。代码将更具可读性

val resources: Seq[Resources] = Seq(
  File("ex1.Scala",Some(Folder("example",Some("~/Dev")))),
  Folder("temp"),
  Folder("bin",Some("/usr")),
  File(".Clouder")
)

你能提供你得到的错误和触发它的代码行吗?你的
Seq
named
resources
必须在每个元素后面包含逗号。仅转到行定义另一项是不够的。是,附加错误SS!请发表你的错误,而不是你错误的照片。我们想读它,而不是欣赏它对色彩和透视的运用。