Is there a way for the this keyword in a super class to refer to that class's subclass? Specifically, I am trying to do the following (the Json refers to Play's Json library):
abstract class A() {
def toJson[T](implicit writes: Writes[T]): JsValue = Json.toJson(this)
}
case class B(myProperty: String) extends A
object B { implicit val bFormat = Json.format[B] }
This gives the error No Json serializer found for type A. Try to implement an implicit Writes or Format for this type.. So it's saying it can't serialize an object of type A, which makes sense. The goal, however, is for the this in Json.toJson(this) to refer to the subclass (which, in this instance, is B).
Is there any way to accomplish this? If not, is there any other way I can implement the Json.toJson(...) method in the superclass without having to implement in the subclass (and all other subclasses of A)?
The common trick to refer to the current subclass from the parent, is to use F-bounded polymorphism:
// Here `T` refers to the type of the current subclass
abstract class A[T <: A[T]]() {
this: T =>
def toJson(implicit writes: Writes[T]): JsValue = Json.toJson(this)
}
// We have to specify the current subclass in `extends A[B]`
case class B(myProperty: String) extends A[B]
object B { implicit val bFormat = Json.format[B] }
println(B("foo").toJson)
This won't allow you to call toJson for any generic A though:
val a: A[_] = B("foo")
println(a.toJson) // Doesn't compile with:
// No Json serializer found for type _$1.
// Try to implement an implicit Writes or Format for this type.
To fix this you would have to save Writes for the subtype at the point of object creation:
abstract class A[T <: A[T]](implicit writes: Writes[T]) {
this: T =>
def toJson: JsValue = Json.toJson(this)
}
Or alternatively using the context bound notation:
abstract class A[T <: A[T] : Writes] {
this: T =>
def toJson: JsValue = Json.toJson(this)
}
And since this F-bounded polymorphism thing is just an implementation detail and always refering to a generic A as A[_] is quite boilerplate-y, you can move this code to an intermediate abstract class.
So a full example looks like this:
abstract class A() {
def toJson: JsValue
}
abstract class AImpl[T <: AImpl[T] : Writes] extends A {
this: T =>
def toJson: JsValue = Json.toJson(this)
}
case class B(myProperty: String) extends AImpl[B]
object B { implicit val bFormat: Format[B] = Json.format[B] }
val a: A = B("foo")
println(a.toJson)
Related
I am trying to provide an extension to a class I can't modify using an implicit class. I have a trait HasFoo[A] that takes a type parameter. I then have a class (Processor) that expects an A that implements HasFoo[A]. The issue is that the compiler doesn't recognize that the Bar class is implementing HasFoo[Bar] via the implicit class extension.
The error is Type argument Bar does not conform to upper bound Playground.HasFoo[Bar]
Is there a way to get the compiler to recognize that the trait HasFoo[Bar] is implemented by the implicit class, or is there a better way to do this?
// Bar is an autogenerated class that I can't modify directly
final case class Bar(baz: String)
trait HasFoo[A] {
def foo: A
}
implicit class Ext(val bar: Bar) extends HasFoo[Bar] {
def foo: Bar = bar.copy(baz = s"${bar.baz} else")
}
class Processor[A <: HasFoo[A]]() {}
// This errors out because type `Bar` doesn't implement `foo`,
// even though the implicit extension does.
val process = new Processor[Bar]()
The class Bar implementing the method def foo from the trait HasFoo doesn't make Bar a subtype of HasFoo.
You can try to make HasFoo a type class rather than just OOP trait (replacing subtype polymorphism and F-bounded polymorphism with ad hoc polymorphism). Third-party classes like autogenerated Bar that can't be modified is exactly a use case for type classes.
// autogenerated class
final case class Bar(baz: String)
// type class
trait HasFoo[A] {
def foo(a: A): A
}
// extension method
implicit class Ext[A: HasFoo](a: A) {
def foo: A = implicitly[HasFoo[A]].foo(a)
}
// Bar is an instance of the type class
implicit val barHasFoo: HasFoo[Bar] = new HasFoo[Bar] {
override def foo(bar: Bar): Bar = bar.copy(baz = s"${bar.baz} else")
}
Bar("baz").foo
// replacing F-bound with context bound
class Processor[A: HasFoo]
val process = new Processor[Bar]
Some intros to type classes:
What are type classes in Scala useful for?
https://kubuszok.com/2018/implicits-type-classes-and-extension-methods-part-1/
https://tpolecat.github.io/2013/10/12/typeclass.html https://tpolecat.github.io/2015/04/29/f-bounds.html
https://books.underscore.io/shapeless-guide/shapeless-guide.html#sec:generic:type-classes (chapter 3.1)
https://www.baeldung.com/scala/type-classes
https://docs.scala-lang.org/scala3/book/types-type-classes.html
https://docs.scala-lang.org/scala3/reference/contextual/type-classes.html
https://gist.github.com/BalmungSan/c19557030181c0dc36533f3de7d7abf4#typeclasses
In principle you can have both type class and OOP trait (for example if you already have many implementations of the OOP trait and you don't want to modify them) although this seems to be overengineering
// autogenerated class
final case class Bar(baz: String)
// OOP trait
trait HasFoo[A] {
def foo: A
}
class HasFooImpl extends HasFoo[HasFooImpl] {
override def foo: HasFooImpl = new HasFooImpl
}
// type class
trait HasFooTC[A] {
def foo(a: A): A
}
implicit class Ext[A: HasFooTC](a: A) extends HasFoo[A] {
override def foo: A = implicitly[HasFooTC[A]].foo(a)
}
// all (F-bounded) implementations of the OOP trait are instances of the type class
implicit def hasFooSubtypes[A <: HasFoo[A]]: HasFooTC[A] = new HasFooTC[A] {
override def foo(a: A): A = a.foo
}
implicit val barHasFoo: HasFooTC[Bar] = new HasFooTC[Bar] {
override def foo(bar: Bar): Bar = bar.copy(baz = s"${bar.baz} else")
}
Bar("baz").foo // extension method
new HasFooImpl().foo // OOP method
class Processor[A: HasFooTC]
val process = new Processor[Bar]
val process1 = new Processor[HasFooImpl]
An alternative to the type class is another pattern, the magnet. Magnets are much less popular than type classes (implicit conversions should be used with caution although the above implicit class actually defined an implicit conversion too)
import scala.language.implicitConversions
// autogenerated class
final case class Bar(baz: String)
// magnet
trait HasFoo[A] {
def foo: A
}
// implicit conversion from Bar to the magnet
implicit def fromBar(bar: Bar): HasFoo[Bar] = new HasFoo[Bar] {
override def foo: Bar = bar.copy(baz = s"${bar.baz} else")
}
Bar("baz").foo
// replacing F-bound with view bound
class Processor[A](implicit ev: A => HasFoo[A])
val process = new Processor[Bar]
Group TypeClass instances by type parameter
Overloading methods based on generics
Type erasure problem in method overloading
How to make a typeclass works with an heterogenous List in scala
Generic function where the return type depends on the input type in Scala?
Problem with bringing into scope scala implicit conversions
https://kubuszok.com/2018/implicits-type-classes-and-extension-methods-part-3/#magnet-pattern
Scala generic method - No ClassTag available for T - when using Collection
Trying to extract the TypeTag of a Sequence of classes that extend a trait with different generic type parameters
As #LuisMiguelMejíaSuárez advices in comments, alternatively a wrapper can be used. This would also work if F-bounded Processor couldn't be modified.
// autogenerated class
final case class Bar(baz: String)
trait HasFoo[A] {
def foo: A
}
final case class BarWrapper(bar: Bar) extends HasFoo[BarWrapper] {
override def foo: BarWrapper = copy(bar = bar.copy(baz = s"${bar.baz} else"))
}
class Processor[A <: HasFoo[A]]
val process = new Processor[BarWrapper]
I need to provide type class instances for a bunch of case classes all derived from a single trait, but as far as I understand Scala compiler expects an instance for a specific class and doesn't go up the inheritance hierarchy. So this code:
trait Base
sealed trait Child extends Base
case class Concrete() extends Child
trait Printable[A] {
def print(value: A): String
}
object WtfTrait {
def print[A](x: A)(implicit ev: Printable[A]) = {
println(ev.print(x))
}
implicit val printableBase = new Printable[Base] {
override def print(value: Base): String = value.toString
}
val x = Concrete()
print(x)
}
doesn't compile with an error reading could not find implicit value for parameter ev: Printable[Impl]. Is there a way to define a single type class instance for the base trait and avoid repitition maybe by using Shapeless or something.
Guess you mean Printable[Concrete] (that's to say a Show typeclass instance).
Need to update to printableBase definition as bellow:
trait Base
sealed trait Child extends Base
case class Concrete() extends Child
trait Printable[A] {
def print(value: A): String
}
object WtfTrait {
def print[A](x: A)(implicit ev: Printable[A]) = {
println(ev.print(x))
}
// HERE
implicit def printableBase[T <: Base] = new Printable[T] {
override def print(value: T): String = value.toString
}
val x = Concrete()
print(x)
}
Printable can be made contravariant by adding a - sign:
trait Printable[-A]
This makes Printable[X] a subtype of Printable[Y] if Y is a subtype of X. In particular, Printable[Base] is a subtype of Printable[Concrete] and can be used when the compiler looks for an implicit of that type.
I use the factory pattern with generics. The idea is to create the right implementation (BlockType1Impl or BlockType2Impl) depending on the type of A which is a case class( BlockchainType1 or BlockchainType2). I don't put any Type Bounds constraints.
Code
After looking at this example on the apply method with generic types
trait BlockTypeFactory[A]{
def findTransactions( blocks: Seq[A], address: String): Seq[TransactionResponse]
}
object BlockTypeFactory{
// I want this method to return the correct implementations
def getBlockExplorer[A](blockType: A): BlockTypeFactory[A] = {
blockType match {
case type1: BlockchainType1 => new BlockTypeFactory[BlockchainType1](new BlockType1Impl)
// error : Expression of type BlockTypeFactory[BlockType1Impl] doesn't conform with the expected type BlockTypeFactory[A]
case type2: BlockchainType2 => new BlockType2Impl
}
}
def apply[A](implicit ev: BlockTypeFactory[A],blockType: A):BlockTypeFactory[A] = ev
}
But I get an error about expected type . What is exactly wrong ?
Other classes
class BlockType1Impl extends BlockTypeFactory[BlockchainType1]
class BlockType2Impl extends BlockTypeFactory[BlockchainType2]
case class BlockchainType1(...)
case class BlockchainType2(...)
Your code doesn't work because the compiler doesn't know where to get the implicit instances of BlockTypeFactory.
In order to achieve your goal you can use Type Classes.
This way is extensible, you can have more than one factory per class if you want (you need to play with implicits scope) and you can define standard factories for some types.
You can code implicit instances of your case classes inside BlockTypeFactory object, but this is the way it is usually done.
// your type class
trait BlockTypeFactory[A] {
def create:A
}
case class BlockchainType1()
object BlockchainType1 {
// type 1 impl
implicit val factory:BlockTypeFactory[BlockchainType1] = new BlockTypeFactory[BlockchainType1] {
def create: BlockchainType1 = BlockchainType1()
}
}
case class BlockchainType2()
object BlockchainType2 {
// type 2 impl
implicit val factory:BlockTypeFactory[BlockchainType2] = new BlockTypeFactory[BlockchainType2] {
def create: BlockchainType2 = BlockchainType2()
}
}
object BlockTypeFactory {
// get factory
def apply[A:BlockTypeFactory]:BlockTypeFactory[A] = implicitly[BlockTypeFactory[A]]
// or create
def create[A:BlockTypeFactory]:A = implicitly[BlockTypeFactory[A]].create
}
val instance1 = BlockTypeFactory[BlockchainType1].create
val instance2 = BlockTypeFactory.create[BlockchainType2]
This pattern is called Type Class, and it is used to get ad hoc polymorphism. In your example, you need a polymorphic method findTransactions for each class defined on BlockTypeFactory.
I have a generic method which a generic type parameter T which is a subclass of MyClass. Inside that method, I want to create e new instance of T, how can I do that?
This doesn't work (because of type erasure):
object Demo extends App {
def myMethod[T <: MyClass](): Unit = {
val t = new T // gives error: class type required by T found
}
myMethod[MySubclassA]()
}
abstract class MyClass
class MySubclassA extends MyClass
class MySubclassB extends MyClass
It fails to work, but not (primarily) because of type erasure, but because your definition should make sense for all T which satisfy the type bounds, and new T doesn't. E.g. T can be MyClass or an abstract subclass, or a subclass without a parameter-less constructor, or a trait (traits can extend classes), or...
If a runtime error is good enough, you can go with Sergey Lagutin's solution. But more reasonable for most cases would be to pass some way to create a T to myMethod. Possibly as an implicit argument, e.g.
class Factory[T](f: () => T) {
def make(): T = f()
}
object Factory {
implicit val MySubclassAFactory =
new Factory(() => new MySubclassA)
implicit val MySubclassBFactory =
new Factory(() => new MySubclassB)
}
def myMethod[T <: MyClass](implicit factory: Factory[T]): Unit = {
val t = factory.make()
...
}
myMethod[MySubclassA] // works
myMethod[MyClass] // compilation error
myMethod[ASubclassForWhichFactoryIsNotDefined] // compilation error
You may use ClassTag to achieve your goal:
def myMethod[T <: MyClass : ClassTag]: T =
implicitly[ClassTag[T]].runtimeClass.newInstance().asInstanceOf[T]
println(myMethod[MySubclassA]) // MySubclassA#66d2e7d9
println(myMethod[MySubclassB]) // MySubclassB#1efbd816
I have 3 classes:
class AClass
class Base { val a = "a" }
class BaseOne extends Base { val b = "b" }
class BaseTwo extends Base { val c = "c" }
I want to extend a trait which contains a generic method, I'm not allowed to change the trait
trait Converter {
def toAClass[T <: Base](e: T): AClass
def fromAClass[T <: Base](s: AClass): T
}
I want to extend it in several different objects
object ConverterBaseOne extends Converter {
// ERROR
override def toAClass(e: BaseOne) : AClass = { printf(e.b) } // b is known
override def fromAlcass(a: AClass) : BaseTwo = {....}
}
I know there is a way to do it with class parameter: trait Converter[T <: Base]
and also saw this post https://stackoverflow.com/a/4627144/980275
I'm asking if there is a solution without changing the trait ???
Thank you
You are changing the signature of the method, so it is not a legal override, it would break polymorphism. You must either parametrize the Converter trait or use another method name.
You can, however, receive a Base object and cast it, but it is not recommended practice since it may result in an exception at runtime:
object ConverterBaseOne extends Converter {
override def toAClass[T <: Base](e: T): AClass = {
printf(e.asInstanceOf[BaseOne].b)
// ...
}
}