score:5
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.
score:1
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>
score:3
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);
}
Source: stackoverflow.com
Related Query
- How do I create an explicit companion object for a case class which behaves identically to the replaced compiler provided implicit companion object?
- Scala: order of definition for companion object vs case class
- In Scala, how can I define a companion object for a class defined in Java?
- Case class companion object generation error for compound type
- In ScalaPb, How to create a case object and case class extending from the same trait?
- How to create Dataset with case class Type Parameter ? (Unable to find encoder for type T)
- Generate companion object for case class with methods (field = method)
- Which is faster for comparing case class object in scala : a) equals(==) method or b) equating hash values
- How to create mock object of a class which is package private
- how to use repl to create instance of a case class defined in object
- Trouble creating a companion object for a case class in Scala
- how to create a Form from an object of case class
- Create a companion object that mixes in a trait that defines a method which returns an object of the object's companion class
- How can we create case class object from json in scala + play framework 2.0
- How to override apply in a case class companion
- SLICK How to define bidirectional one-to-many relationship for use in case class
- How to write a Play JSON writes converter for a case class with a single nullable member
- Case class and companion object
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- How to check which parameters of case class have default value using scala reflection 2.10
- How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?
- Compile error when using a companion object of a case class as a type parameter
- How do I create a class or object in Scala Macros?
- Mapped projection with <> to a case class with companion object in Slick
- How to deprecate the companion object of a case class?
- An object extends its companion case class in Scala
- Companion object in Scala isn't associating itself with case class
- How can provide JsonFormats for case class that references itself?
- automatically generate case object for case class
- How do I write a scala extractor for a case class with default parameters?
More Query from same tag
- Simple Akka mailbox configuration to discard overflowing messages
- Can Akka (Scala) be used to create asynchronous persistent server?
- Scala REPL issue - Multiexpression issue
- How to POST a Java POJO / Json to a Play Framework v2.2.1 Controller Http POST Method
- Scala/Java template list iteration and concatenation of string
- Is there a build in "slow" Future.traverse version?
- eclipse: how to debug a Scala program called from a shell script
- How can I use Processing functions in other Scala classes?
- How do I publish the artifact generated from assemblyPackageDependency
- Convert a traversable to another at run time in scala
- How to resolve log4javascript JS libraries in ScalaJS project
- Scala: How to return a Some or Option
- Create new DataFrame with new rows depending in number of a column - Spark Scala
- Why is a Scala companion object compiled into two classes(both Java and .NET compilers)?
- How to get item id from cosine similarity matrix?
- Convert an array to a mutable set in Scala?
- How to improve grouping by head
- Scala style with Futures
- Scala GUI - Event Handling
- Spark - Scala : Return multiple <key, value> after processing one line
- Use a Actor as a source to my Websocket client flow
- convenience method in spray (soon to be akka-http) to create a Location header w/ host port & contextRoot ?
- Questions about placeholders in Scala
- How to sort strings in Scala with complex criteria?
- Custom logic for matching with Scala parser combinators
- Publish zip created by sbt-native-packager
- Type alias for immutable collections
- How to get the creation date of a file that is present on HDFS?
- How to do handle this use-case (running-window data) in spark
- Safely Get Tail of Array