Using trait method in the class constructor - scala

I have a trait and a class that extends the trait. I can use the methods from the trait as follows:
trait A {
def a = ""
}
class B(s: String) extends A {
def b = a
}
However, when I use the trait's method in the constructor like this:
trait A {
def a = ""
}
class B(s: String) extends A {
def this() = this(a)
}
then the following error appears:
error: not found: value a
Is there some way to define default parameters for the construction of classes in the trait?
EDIT: To clarify the purpose: There is the akka-testkit:
class TestKit(_system: ActorSystem) extends { implicit val system = _system }
And each test looks like this:
class B(_system: ActorSystem) extends TestKit(_system) with A with ... {
def this() = this(actorSystem)
...
}
because I want to create common creation of the ActorSystem in A:
trait A {
val conf = ...
def actorSystem = ActorSystem("MySpec", conf)
...
}

It's a little bit tricky because of Scala initialization order. The simplest solution I found is to define a companion object for your class B with apply as factory method:
trait A {
def a = "aaaa"
}
class B(s: String) {
println(s)
}
object B extends A {
def apply() = new B(a)
def apply(s: String) = new B(s)
}

Related

Magnolia: Type derivation fails in case of nested type classes

I am trying to create a serializable trait that has a dependency on type class.
package dsl
import zio.schema._
sealed trait Random[A] {
def generate: A
}
object Random {
case object RandomDouble extends Random[Double] {
override def generate: Double = ???
implicit val RandomDoubleSchema: Schema[Random[Double]] = DeriveSchema.gen[Random[Double]]
}
}
sealed trait DummyExpr[A] {
def eval(value: DummyExpr[A]): A
}
object DummyExpr {
case object DummyTrue extends DummyExpr[Boolean] {
override def eval(value: DummyExpr[Boolean]): Boolean = ???
}
case object DummyFalse extends DummyExpr[Boolean] {
override def eval(value: DummyExpr[Boolean]): Boolean = ???
}
case class DummyOperator[A](random: Random[A], predicate: DummyExpr[Boolean]) extends DummyExpr[A] {
override def eval(value: DummyExpr[A]): A = ???
}
}
object main extends App {
val schemaRandom = DeriveSchema.gen[Random[Double]]
val schemaDummy = DeriveSchema.gen[DummyExpr[Double]]
}
Here is a reproducible link https://scastie.scala-lang.org/3PnmF52hSkuduzGP10wTdg
But type derivation for this fails with the error magnolia: could not find any direct subtypes of trait Random
I am using zio-schema which internally uses magnolia. I tried adding implicit Derivation of typeclass too but that didn't help too.

Scala: How to avoid using the existential type

Suppose I have a trait defined like
trait SomeTrait[A] {
def doSomething(): Seq[A]
}
and multiple classes that extend this trait as follows.
class SomeClass extends SomeTrait[SomeType] {
def doSomething(): Seq[SomeType] = {
:
}
}
Now in another class, I want to store the collection of instances of the classes extending the trait.
class AnotherClass {
pirvate val someClassInstances = mutable.Buffer[SomeTrait[_]]()
def addSomeClass[A](sc: SomeTrait[A]): Unit = {
this.someClassInstances += sc
}
}
How can I avoid using the existential type here?
Does the following meet your needs:
import scala.collection.mutable
trait SomeTrait[A] {
def doSomething(): Seq[A]
}
class SomeClass extends SomeTrait[Int] {
def doSomething(): Seq[Int] = ???
}
class AnotherClass {
private val someClassInstances = mutable.Buffer[SomeTrait[_]]()
def addSomeClass(sc: SomeTrait[_]): Unit = {
this.someClassInstances += sc
}
}
?

Scala type class can't find implicit instances

I am trying to create a type class in Scala and use it to make a simple polymorphic case class. This example doesn't compile and gives "could not find implicit value for parameter writer:A$A228.this.ValueWriter[T]". I can't really figure out what could be going wrong or where to start.
trait Keeper
case class StringKeeper(measure: String) extends Keeper
case class StringLengthKeeper(measure: Int) extends Keeper
trait ValueWriter[A] {
def write(value: String): A
}
object DefaultValueWriters {
implicit val stringWriter = new ValueWriter[StringKeeper] {
def write(value: String) = StringKeeper(value)
}
implicit val stringLengthWriter = new ValueWriter[StringLengthKeeper] {
def write(value: String) = StringLengthKeeper(value.length)
}
}
object Write {
def toWrite[A](value: String)(implicit writer: ValueWriter[A]) = {
writer.write(value)
}
}
case class WriterOfKeepers[T <: Keeper](value: String) {
def run: T = {
Write.toWrite[T](value)
}
}
import DefaultValueWriters._
val writerLengthKeeper = WriterOfKeepers[StringLengthKeeper]("TestString")
writerLengthKeeper.run
There is no implicit ValueWriter in scope when you call Write.toWrite.
You need to have the implicit parameter in the constructor
case class WriterOfKeepers[T <: Keeper : ValueWriter](value: String) {
def run: T = {
Write.toWrite[T](value)
}
}
or in the method
case class WriterOfKeepers[T <: Keeper](value: String) {
def run(implicit writer: ValueWriter[T]): T = {
Write.toWrite(value)
}
}
or find some other way compatible with your requirements (and I don't know those).

Spray: Overriding functions in Traits

I'm using Spray and have a Base Route trait which defines a number of functions that should be implemented...
trait ServiceBaseRoute extends HttpService {
def function1():Type
def function2():Type
lazy val serviceBaseRoute = ...
I then mixin this trait to a number of other traits...
trait MyRoute1 extends HttpService
with ServiceBaseRoute {
override def function1():Type = {...}
override def function2():Type = {...}
val myRoute1 = serviceBaseRoute
and...
trait MyRoute2 extends HttpService
with ServiceBaseRoute {
override def function1():Type = {...}
override def function2():Type = {...}
val myRoute2 = serviceBaseRoute
Finally I construct the top level of the route as follows...
trait V1Routes extends HttpService
with MyRoute1
with MyRoute2 {
val v1Routes =
pathPrefix("v1") {
authenticate(...) {
myRoute1 ~ myRoute2
}
}
Compiles fine etc... However when I run this the function overrides of MyRoute2 override those defined in MyRoute1. I am thinking this is because I don't have actual instances of MyRoute1 and MyRoute2 as they are being simply mixed in and since MyRoute2 is added in after MyRoute1 its values overwrite those of MyRoute1?
So what would be the best way to implement this preserving the overrides defined in each trait?
Do I need to define MyRoute1 and MyRoute2 as objects instead?
Thanks
Based on your comments, the aim is to have distinct routes in MyRoute1 and MyRoute2, where each is created by customizing serviceBaseRoute. Since ServiceBaseRoute is customized by two functions, you could make serviceBaseRoute a higher order function and pass the implementations of the two functions to it in the sub traits:
trait ServiceBaseRoute extends HttpService {
def serviceBaseRoute(f1: () => Type, f2: () => Type)= ???
}
trait MyRoute1 extends HttpService
with ServiceBaseRoute {
def f1ImplA(): Type = ???
def f2ImplA(): Type = ???
val myRoute1 = serviceBaseRoute(f1ImplA, f2ImplA)
}
trait MyRoute2 extends HttpService
with ServiceBaseRoute {
def f1ImplB(): Type = ???
def f2ImplB(): Type = ???
val myRoute2 = serviceBaseRoute(f1ImplB, f2ImplB)
}
trait V1Routes extends HttpService
with MyRoute1
with MyRoute2 {
val v1Routes =
pathPrefix("v1") {
authenticate(...)
{
myRoute1 ~ myRoute2
}
}
}

Scala implicit type class dependency injection

I'd like some help sorting out this scenario. I have an Akka actor where I want to inject a dependency, in this case RemoteFetcher, which I would also like mock in my tests. Like so:
main/src/scala/mypackage/Services.scala
package mypackage
import RemoteFetcherFileSystem._
trait RemoteFetcher {
def fetch( path:String ): Future[Stream[String]]
}
class MyRemoteResourceActor extends Actor with ActorLogging {
def fetchRemote( path:String ) = implicitly[RemoteFetcher].fetch( path )
def receive = {
case FetchRemoteResource( path ) => fetchRemote( path ).map( _.foreach( sender ! _ ) )
}
}
For this to work I have an implicit object that I import into the file above. Would look something like this:
implicit object RemoteFetcherFileSystem extends RemoteFetcher {
def fetchRemote( path:String ) = Future[Stream[String]] { ... reading from file system ... }
}
Now in my tests I have TestActor from the akka-testkit. Here I want to instead import my mock dependency:
implicit object RemoteFetcherMock extends RemoteFetcher {
def fetchRemote( path:String ) = Future[Stream[String]] { ... mock implementation ... }
}
My problem is that to compile Services.scala I need to import the implicit object. But how do I go about to shadow/override this in my test-files. The reason I'm not using implicit arguments is that I want to avoid having to modify all my actors constructor arguments.
I when looking around and reading up on the type class dependency injection pattern and I get it to work according to the tutorials, but I don't get it to work when I want to test and override like in my example.
I'm not sure how to do it with implicits, but typically one could inject instead like so:
trait RemoteFetcherComponent {
def remoteFetcher: RemoteFetcher
trait RemoteFetcher {
def fetch(path: String): Future[Stream[String]]
}
}
trait RemoteFetcherFileSystemComponent extends RemoteFetcherComponent {
val remoteFetcher = RemoteFetcherFileSystem
object RemoteFetcherFileSystem extends RemoteFetcher {
def fetch(path: String): Future[Stream[String]] = ???
}
}
class MyRemoteResourceActor extends Actor with ActorLogging with RemoteFetcherFileSystemComponent {
def fetchRemote(path: String) = remoteFetcher.fetch(path)
def receive = {
case FetchRemoteResource(path) => fetchRemote(path).map( _.foreach(sender ! _))
}
}
val myRemoteResourceActor = new MyRemoteResourceActor()
And then a test value would be defined like so:
trait RemoteFetcherMockComponent extends RemoteFetcherComponent {
def remoteFetcher = RemoteFetcherMock
object RemoteFetcherMock extends RemoteFetcher {
def fetch(path: String): Future[Stream[String]] = ???
}
}
val myMockedResourceActor = new MyRemoteResourceActor with RemoteFetcherMockComponent {
override val remoteFetcher = super[RemoteFetcherMockComponent].remoteFetcher
}
The reason you are having an issue with implicits is because the way you're using it is no different from simply using def fetchRemote(path: String) = RemoteFetcherFileSystem.fetch(path). With the import, you've defined the implementation, rather than allowed it to be injected later.
You could also change the implicitly to an implicit parameter:
trait RemoteFetcher {
def fetch(path: String): Future[Stream[String]]
}
object RemoteFetcher {
implicit val fetcher = RemoteFetcherFileSystem
}
class MyRemoteResourceActor extends Actor with ActorLogging {
def fetchRemote(path: String)(implicit remoteFetcher: RemoteFetcher) = remoteFetcher.fetch(path)
def receive = {
case FetchRemoteResource(path) => fetchRemote(path).map( _.foreach(sender ! _))
}
}
Then you could override the implicit that is resolved in the companion object of RemoteFetcher by simply importing RemoteFetcherMock.
See this post for more information about implicit parameter resolution precedence rules.