score:8
Scala extractor companion objects should extend Function
For case classes, the compiler generates a companion object under the hood. This holds relevant static methods for the Wrapper case class, including apply. Peering at the bytecode, we discover that Wrapper's companion object extends Function1. (Actually it extends AbstractFunction1, which uses @specialized to obviate autoboxing.)
This is also noted here. Why do case class companion objects extend FunctionN?
When replacing our Wrapper case class with an extractor, we should take care to extend our home grown companion object with AbstractFunction1 to preserve backwards compatibility for our clients.
This amounts to a small tweak in the source code. The apply method doesn't change at all.
object Wrapper extends scala.runtime.AbstractFunction1[Int, Wrapper] {
score:12
Simply put, Wrapper
(the object) isn't a Function, it's just an object with an apply method. This has absolutely nothing to do with the object also being an extractor.
Try this:
class Wrapper(val x: Int) {
override def toString = "Wrapper(" + x + ")"
// other methods elided
}
object Wrapper extends (Int => Wrapper) {
def apply(x: Int) = new Wrapper(x)
def unapply(w: Wrapper): Option[Int] = Some(w.x)
}
def higherOrder(f: Int => Wrapper) = println( f(42) )
I also made the parameter to Wrapper
a val
and swapped around the parameter and return value in your definition of unapply
so that it would match case-class behaviour.
Source: stackoverflow.com
Related Query
- Why did replacing my Scala case class with an extractor break my higher order function?
- Scala collections: why do we need a case statement to extract values tuples in higher order functions?
- Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala
- How do I write a scala extractor for a case class with default parameters?
- Why and how to migrate case class to extractor in Scala
- Higher Order Function with Case Class Method
- why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?
- Why Scala case class copy method parameterised only with the variables defined in the case class?
- Case Class with higher order function in Parameter
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class extending Product with Serializable
- Shapeless - turn a case class into another with fields in different order
- Read CSV in Scala into case class instances with error handling
- Why not make every Scala class a case class?
- Scala copy case class with generic type
- Scala wont pattern match with java.lang.String and Case Class
- Derived Scala case class with same member variables as base
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- Problem with bounded type parameterised case class and default args in Scala
- How do I add a no-arg constructor to a Scala case class with a macro annotation?
- Higher order operations with flattened tuples in scala
- Scala case class with function parameters
- compare case class fields with sub fields of another case class in scala
- Why does scala allow private case class fields?
- Why don't Scala case class fields reflect as public?
- Modeling with Scala case class
- Using new with Scala final case class
- Slick 2.10-RC1, Scala 2.11.x, bypassing 22 arity limit with case class (heterogenous)
- Scala case class copy doesn't always work with `_` existential type
- Scala case class copy constructor with dynamic fields
More Query from same tag
- Type parameter bounds with Spark objects are hard to get
- Match a tuple of unknown size in scala
- Apache Spark: How do I convert a Spark DataFrame to a RDD with type RDD[(Type1,Type2, ...)]?
- Monitoring Tools for Play Framework Application
- How to merge an Object and non-object-valued in HOCON?
- spark shell column multiplication and updating same dataframe
- Compilation error following Play! Framework NewApplication guide
- Why Akka shutdowns dispatcher, when there is no tasks?
- merging multiple levels of configuration using typesafe/akka config
- Spark MLLib Linear Regression model intercept is always 0.0?
- .eq causing warning. How do I get rid of it?
- Correct use of if condition with in map in scala
- def fn[String] seems to break Scala / java.lang.String compatibility
- Akka logging pattern unchangeable
- Validate JSON in Gatling performance test
- Trying to write DataFrame to csv file
- How to set amount of Spark executors?
- How can I call a method defined on a Scala package object from Java? (Scala 2.10.x)
- ContactPoints vs ContactPoint in Phantom-DSL
- NoClassDefFoundError: akka/actor/ActorRefFactory in Scala
- What is the usage of an `implicit def` without arguments in Scala?
- Scala - Using anonymous function shorthand for successive map() calls
- How to flatten a Try[Option[T]]
- How to concat two form data in akka scala?
- How to seperate list elements if key is not present using scala?
- Update stream on file change
- Can't import scala.reflect.runtime.universe
- Companion objects in scala
- Scala object that extends Java class
- Convert EitherT[Future, A, B] to Option[B]