How to pattern match large Scala case classes?

前端 未结 4 2031
攒了一身酷
攒了一身酷 2020-12-05 01:43

Consider the following Scala case class:

case class WideLoad(a: String, b: Int, c: Float, d: ActorRef, e: Date)

Pattern matching allows me

4条回答
  •  自闭症患者
    2020-12-05 02:22

    I don't know if this is appropriate, but you can also build an object just to match that field, or that set of fields (untested code):

    object WideLoadActorRef {
      def unapply(wl: WideLoad): Option[ActorRef] = { Some(wl.d) }
    }
    
    someVal match {
      case WideLoadActorRef(d) => d ! someMessage
    }
    

    or even

    object WideLoadBnD {
      def unapplySeq(wl: WideLoad): Option[(Int,ActorRef)] = { Some((wl.b,wl.d)) }
    }
    
    someVal match {
      case WideLoadBnD(b, d) => d ! SomeMessage(b)
    }
    

提交回复
热议问题