2016-11-08 35 views
0

考虑斯卡拉(SBT)验证码:如何循环遍历Scala中的Sequential元素?

abstract class Farm 
case class Pig (length:Int, height:Int) extends Farm 
val barn1 = Sequential (List (Pig (50 , 30), Pig (55 , 32))) 

现在我想通过barn1定义循环功能:

def playSequential (?1):Unit = { 
    ?2 
} 

我要补充,而不是?

  • ?1:哪种输入类型(参见k:Int我需要
  • ?2:???如何定义一个for循环遍历barn1

回答

2

也许你想这样的事情

abstract class Farm 
case class Pig (length:Int, height:Int) extends Farm 
val barn1 = List(Pig (50 , 30), Pig (55 , 32)) 

def playSequential(barn: List[Farm]):Unit = { 
    barn.foreach{ 
    case Pig(l,h) => // do something with length (l) and height (h) of this Pig 
    case Cow(l,h) => // do something with length (l) and height (h) of this Cow 
    case _ =>   // unknown element, report error 
    } 
} 

我不知道什么Sequential是(它不是标准的斯卡拉),所以我删除它。