I have a case class defined as such:
case class StreetSecondary(designator: String, value: Option[String])
I then define an expli
Scala 2.11.0
It looks like scalac is predefining the tupled
function when you aren't providing additional functions in the companion
case class BB(a: Int, b: Int)
object BB { }
case class AA(a: Int, b: Int)
object CC { }
case class CC(a: Int, b: Int)
results in the following
public class AA implements scala.Product,scala.Serializable {
public static scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(AA);
public static AA apply(int, int);
public static scala.Function1<scala.Tuple2<java.lang.Object, java.lang.Object>, AA> tupled();
public static scala.Function1<java.lang.Object, scala.Function1<java.lang.Object, AA>> curried();
public int a();
public int b();
public AA copy(int, int);
public int copy$default$1();
public int copy$default$2();
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public scala.collection.Iterator<java.lang.Object> productIterator();
public boolean canEqual(java.lang.Object);
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public AA(int, int);
}
public final class AA$ extends scala.runtime.AbstractFunction2<java.lang.Object, java.lang.Object, AA> implements scala.Serializable {
public static final AA$ MODULE$;
public static {};
public final java.lang.String toString();
public AA apply(int, int);
public scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(AA);
public java.lang.Object apply(java.lang.Object, java.lang.Object);
}
public class BB implements scala.Product,scala.Serializable {
public static scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(BB);
public static BB apply(int, int);
public int a();
public int b();
public BB copy(int, int);
public int copy$default$1();
public int copy$default$2();
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public scala.collection.Iterator<java.lang.Object> productIterator();
public boolean canEqual(java.lang.Object);
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public BB(int, int);
}
public final class BB$ implements scala.Serializable {
public static final BB$ MODULE$;
public static {};
public BB apply(int, int);
public scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(BB);
}
public class CC implements scala.Product,scala.Serializable {
public static scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(CC);
public static CC apply(int, int);
public int a();
public int b();
public CC copy(int, int);
public int copy$default$1();
public int copy$default$2();
public java.lang.String productPrefix();
public int productArity();
public java.lang.Object productElement(int);
public scala.collection.Iterator<java.lang.Object> productIterator();
public boolean canEqual(java.lang.Object);
public int hashCode();
public java.lang.String toString();
public boolean equals(java.lang.Object);
public CC(int, int);
}
public final class CC$ implements scala.Serializable {
public static final CC$ MODULE$;
public static {};
public CC apply(int, int);
public scala.Option<scala.Tuple2<java.lang.Object, java.lang.Object>> unapply(CC);
}
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])] = <function1>
When defining an explicit companion object for a case class (as of Scala 2.11), to fully replace the compiler provided functionality in the lost implicit companion object, the basic template for the explicit companion object has two requirements:
Requirements:
1. Must extend a function definition which consists of a tuple (exactly matching the type and order of the case class constructor parameters) returning the type of the case class
2. Must override the toString function to provide the object class name (identical to that of the associated case class)
Here's the original example code for the "empty" explicit companion object:
object StreetSecondary {
//empty for now
}
And here is the example code after implementing the above requirements:
object StreetSecondary extends ((String, Option[String]) => StreetSecondary) {
//replace the toString implementation coming from the inherited class (FunctionN)
override def toString =
getClass.getName.split("""\$""").reverse.dropWhile(x => {val char = x.take(1).head; !((char == '_') || char.isLetter)}).head
}
To meet requirement 1 above, extends ((String, Option[String]) => StreetSecondary)
is inserted right after the object name and before the first curly brace.
To meet requirement 2 above, override def toString = getClass.getName.split("""\$""").reverse.dropWhile(x => {val char = x.take(1).head; !((char == '_') || char.isLetter)}).head
is inserted in the body of the object (the explicit implementation remains questionable)
Deep appreciation to @drstevens for his posting the javap output to help me gain confidence the above two steps are all that are required to restore the lost functionality.