Usage of new and traits in Scala - scala

I've found usage of the following in Scala examples, but I can't find proper reference (probably because Google gets confused by new and with that are very common words...):
var someInstance = new Class with SomeTrait
In the basic documentation about traits I couldn't find anything...

Well... its a way to create instances of Anonymous class.
So,
trait A
trait B
class C extends A
val cb = new C with B
// creates an instance of anonymous class 'C with B'
is same as,
trait A
trait B
class C extends A
class D extends C with B
val d = new D
// creates an instance of class 'D'
Now, cb and d both will have same properties.

Related

Inherit from a class parametrized by an inner type

I would like to have a class B that inherits from a generic class A that is parametrized by an inner type of B. Specifically, I would like this (minimized example):
class A[T]
class B extends A[T] {
class T
}
Written like this, the compiler does not accept it. Is there any way to specify this inheritance relationship? (Using some different syntax, or some tricks.)
If not, what would be an official reference documenting that this is not possible?
Notes:
Yes, I want T to be an inner class. I do want separate instances of B to have incompatible types T.
This question considers the same situation but does not ask whether/how it is possible, but instead asks for other ways to write the same thing. (At least that's how the answers seem to interpret it.) In any case, that question is ten years old, and Scala may have evolved since.
Clarification: I want B to inherit from A, not from an inner class of A, or have an inner class of B inherit from A. The reason is that I want to use it in the following situation: A is a type class. B is supposed to provide a quick way to easily generate new classes of that type class (with minimal boilerplate). I want to the user of the class to do something like: implicit val X = createBInstance(), and then they will be able to use the type X.T and it will have type class A. The best I managed so far is to support the pattern val X = createBInstance(); implicit val xTypeclass = X.typeclass and use X.T. This means more boilerplate, the possibility to forget something, and it pollutes the namespace more (additional name xTypeclass). (For the very curious: A is MLValue.Converter, B is QuickConverter, and X is Header/RuntimeError/... in my code.)
You can try
class A {
type T
}
class B extends A {
class T
}
val b: B = new B
val t: b.T = new b.T
I made T a type member rather than type parameter.
Here a class overrides a type.
If you want to use T also as a type parameter you can introduce Aux-type
object A {
type Aux[_T] = A { type T = _T }
}
and use type A.Aux[T] as a replacement to your original A[T].
Now you're doing
trait A[X]
class B {
class T
/*implicit*/ object typeclass extends A[T]
}
val b = new B
implicit val b_typeclass: b.typeclass.type = b.typeclass
val b1 = new B
implicit val b1_typeclass: b1.typeclass.type = b1.typeclass
implicitly[A[b.T]]
implicitly[A[b1.T]]
Please notice that making object typeclass implicit is now useless.
Making object typeclass implicit would be useful if you made b, b1 objects rather than values
trait A[X]
class B {
class T
implicit object typeclass extends A[T]
}
object b extends B
object b1 extends B
implicitly[A[b.T]]
implicitly[A[b1.T]]
If you want to make B extend A then as earlier I propose to replace type parameter of A with a type member. A will still be a type class, just type-member type class rather than type-parameter type class
trait A {
type X
}
object A {
type Aux[_X] = A {type X = _X}
}
class B extends A {
type X = T
class T
}
implicit object b extends B
implicit object b1 extends B
implicitly[A.Aux[b.T]]
implicitly[A.Aux[b1.T]]

Mocking with Scala Self Type / Parfait Pattern

I am using parfait pattern like the following:
trait A {
def a: Future[String]
}
class B {
this: A =>
def get: Future[String] = a
}
I would like to create a mocking version of A so I can control the result of def a using Mockito when testing B, is there a way to do that?
While testing a class X which uses an instance of class Y, we generally provide a mocked instance of class Y.
Here, Your B is not using any instance of A, it expects (forces) that anything extending B also extends A.
Also, you will notice that your class B is kind of incomplete as it is. if you choose to create an instance of B,
val b = new B
// <console>:13: error: class B cannot be instantiated because it does not conform to its self-type B with A
// val b = new B
So. basically whenever you are creating an instance of B you have to also provide an implementation of A.
scala> val b = new B with A { def a = Future.successful("well") }
// b: B with A = $anon$1#11ad095c
Notice, how we are free to provided any implementation of A, whenever we are creating any instance of B.
You can provide whatever you require for your test cases this way.
So,
trait AWithMockedBehaviour1 extends A {
def a = Future.successful("mocked_1")
}
val b = new B with AWithMockedBehaviour1

Scala diamond prob, find class name from instance of class D

I have four traits A, B, C and D in the typical diamond problem hierarchy.
I have implemented a method calculate in trait A which checks the instance of callerObject, performs some calculation and returns trait A type object. calculate method is successfully able to check the instanceType of callerObject when callerObject belongs to trait B or C but doesn't work for object of trait D and I get the following class cast exception:
java.lang.ClassCastException: packageName.B$$anon$1 cannot be cast to packageName.D
Can you please suggest way forward, how can I check the Type of the object from trait D in method of trait A.
PS: I am new to Scala.
With pattern matching the order of the case statements matters. Make sure that the most specific class is always matched at the top. For example if B extends A and C extends B, it means that objects of C will always match anything that looks for either B or A, etc.
Here is a toy example that can better explain the solution:
sealed trait A {
def calculate(i: A) = i match {
case _:D => "d" // Make sure the D is checked first!
case _:B => "b"
case _:C => "c"
// If you want to match for A make sure it is added last
}
}
trait B extends A
trait C extends A
trait D extends B with C
Here is an example in the REPL:
val b = new B{}
val c = new C{}
val d = new D{}
b.calculate(b)
res> "b"
b.calculate(c)
res> "c"
b.calculate(d)
res> "d"

What is the difference between "class C extends A with B" and "class C extends B" when trait B extends trait A

When two traits are defined like this,
trait A
trait B extends A
what is the difference between these two.
class C extends B
class D extends A with B
I do not think it is necessary for class C or D to extends trait A since trait B already extends trait A.
Why is this often written "class D extends A with B" ?
It's a pretty good question... I'll try to answer with a subjective response.
I guess that extends A mixin with B is important when the hierarchy will be linearized, in this particular case there is no differences but what if you mixin D with another trait E that reimplements some (but not all) functions from A which aren't well advised in B for your needs in D. So you'd have
class D extends A with E with B
Moreover in that case, we keep the meaning that D is a A

Scala related trait, abstract types

I have 2 related traits. Dao will be used be a class and DaoHelper will be used by Dao's companion object. I would like trait Dao to be able use functions defined in DaoHelper, the only way I could figure out how to do this is to define the companion trait as a val. However somehow companion expects its type to be D.this.T which I thought I has defined as a subtype of Doa. I am confused here. My apologies for the newb question, I come from a dynamic language background.
/test2.scala:14: overriding value companion in trait Dao of type Test.DaoHelper[D.this.T];
[error] value companion has incompatible type
[error] val companion = D
object Test extends App {
trait Dao {
type T <: Dao
val companion: DaoHelper[T]
def getHelpfulData = companion.help
}
trait DaoHelper[Dao] {
val help = "Some helpful data"
}
class D extends Dao {
val companion = D
}
object D extends DaoHelper[D]
}
companion has type DaoHelper[T], but T isn't specified anywhere in D, so how would the compiler know it is supposed to be D in class D? You can fix it by overriding it in D.
class D extends Dao {
type T = D
val companion = D
}
I don't quite understand what you are trying to do with class D, but you are getting this error because you are assigning D to companion in class D, but companion has type DaoHelper[T] as defined in Dao. Since D has type Dao and Dao is not a subtype of DaoHelper[T], this will not work.