How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?

前端 未结 3 1271
独厮守ぢ
独厮守ぢ 2020-12-09 06:41

I have a case class defined as such:

case class StreetSecondary(designator: String, value: Option[String])

I then define an expli

3条回答
  •  独厮守ぢ
    2020-12-09 07:07

    Why would you think you are loosing them?

    case class Person(name: String, age: Int)
    object Person {
      def apply(): Person = new Person("Bob", 33)
    }
    
    val alice = Person("Alice", 20)
    val bob   = Person()
    
    Person.unapply(alice)    //Option[(String, Int)] = Some((Alice,20))
    Person.unapply(Person()) //Option[(String, Int)] = Some((Bob,33))
    

    Seems like I still got extractors.

    In your case you still got it all:

    scala> StreetSecondary.unapply _
    res10: StreetSecondary => Option[(String, Option[String])] = 
    

提交回复
热议问题