I am new to Scala so apologies if its dumb question:
I have some common status that I won't to have enum(trait) for
Trait A
Object A {
case object A1 extends A
case object A2 extends A
val vectA = Vector(A1,A2)
}
I have 2 children who need additional statuses
Trait B extends A
Object B extends A{
case object B1 extends B
case object B2 extends B
val cVect = Vector(B1,B2) ++ A.vectA
def apply(status: String): C = {
cVect.find(_.toString == status).getOrElse("error")
}
}
Trait C extends A
Object C extends A{
case object C1 extends C
case object C2 extends C
val cVect = Vector(C1,C2) ++ A.vectA
def apply(status: String): C = {
cVect.find(_.toString == status).getOrElse("error")
}
}
Basically, I have apply method that will take string and return be Trait of Type B or C
so expecting
val x = B.apply("A1") to return B type enum.
I may be doing something completely wrong so please provide me right approach to deal with such situation
You probably want to do something like this:
sealed trait A
sealed trait ACompanion {
def vect: Vector[A]
final def apply(status: String): Option[A] =
vect.find(_.toString.toLowerCase == status.toLowerCase)
}
object A extends ACompanion {
final case object A1 extends A
final case object A2 extends A
override final val vect: Vector[A] = Vector(A1, A2)
}
sealed trait B extends A
object B extends ACompanion {
final case object B1 extends B
final case object B2 extends B
override final val vect: Vector[A] = Vector(B1, B2) ++ A.vect
}
sealed trait C extends A
object C extends ACompanion {
final case object C1 extends C
final case object C2 extends C
override final val vect: Vector[A] = Vector(C1, C2) ++ A.vect
}
Note that apart from fixing some typos, using sealed and creating a trait for the companions to reduce code duplication. The main change was understanding that vect will always be a Vector[A]; and thus find returns an Option[A].
This is because even for B or C, you are adding extra cases but you need to also provide the original A cases, thus your return type will need to the the least upper bound of those which is just A
You can see the code running here.
Related
I have the following trait and classes:
trait A{
def func1: String
def func2: Int
}
class F extends A{
def func1: String = ???
def func2: Int = ???
}
class G extends A{
def func1: String = ???
def func2: Int = ???
}
And I want to create the following trait and class:
trait X{
val a: ***
}
class Z extends X{
override val a = new G
}
What do I need to write on '***' in trait X so that I can assign val a with a class F or G ?
Thank you.
EDIT
Sorry, I forgot to mention that trait A is A[Any] and traits F and G can be 'F extends A[String]' and 'G extends A[Int]'. In other words, the classes can extends the trait A with different types of parameters.
Re. your edit, your code fails because trait A only takes type parameter of a fixed type. If you declare base class having a A[Any], you cannot override it with a A[Int] or A[String]. There are two ways to make your code works.
One is to make trait A type argument covariant, so that you can override with subclass of the type argument you declared in base class (note the + in trait A definition):
trait A[+T]
class F extends A[String]
class G extends A[Int]
trait X {
val a: A[Any]
}
class Z extends X {
val a = new G
}
See here: Sample
Another way to work is to declare a as a A[_] (_ means it takes any type as a parameter, which is different from Any):
trait A[T]
class F extends A[String]
class G extends A[Int]
trait X {
val a: A[_]
}
class Z extends X {
val a = new G
}
See here: Sample
Because I didn't find a solution to my problem anywhere, I think I might be thinking into a very wrong direction.
Here's my problem:
I have a trait A and another trait B and companion objects AB1, AB2, AB3 and so on. The singleton objects extend trait A and the classes extend trait B. Many objects of those classes are in a list.
On these objects I want to use functions defined in the corresponding singleton object which take objects of the same class as the singleton object is.
This an example for the traits:
trait A {
def compare(firstB: B, secondB: B) : Int
}
trait B {}
And the companion objects:
class AB1(val variable: Int) extends B {}
object AB1 extends A {
def apply(list: List[Int]): Option[AB1] = {
if(list.foldLeft(0)(_ + _) < 10 && list.nonEmpty)
some(new AB1(list.head))
else
null
}
override def compare(ab11: AB1, ab12: AB1): Int = {
if(ab11 > ab12)
1
else if(ab11 > ab12)
-1
else
0
}
}
and
class AB2(val variable1: Int, val variable2: Int) extends B {}
object AB2 extends A {
def apply(list: List[Int]): Option[AB1] = {
if(list.foldLeft(0)(_ + _) < 20 && list.length >= 2)
some(new AB1(list.head, list.tail.head))
else
null
}
override def compare(ab21: AB2, ab22: AB2): Int = {
if(ab11 > ab12)
10
else if(ab11 > ab12)
-10
else
0
}
}
So I've already filtered the some objects and put the "real" objects into a list, let's call it bList. On every element of the list I want to call the compare function.
I think it would look something like this:
val bList: List[B]
val a = getA(bList.head) // getA should be a function that returns the companion object of a class
a.compare(bList.head, bList.tail.head)
On my way I encountered two problems:
b.getClass does not equal AB1.getClass when b is an object the class AB1. But this is not my main problem. I found a solution using String comparison, which really is not pretty, but for now, it works.
The compare function has to be defined in the trait, because otherwise it can't be casted on any singleton object that extends trait A. But I find no way to define the function with parameters of a variable type.
I really hope you can help me with this problem!
EDIT: Now I see that I've forgotten to mention something:
I think that I have to go a bit deeper into what I am trying to do to make you understand my problem:
I have a List[List[C]]. A List of those C's may be capable of creating an AB1 object with it but maybe AB2, or maybe AB3 and so on. So I have
val c: List[C] = (C1, C2, C4)
val aList: List[A] = (AB1, AB2, AB3, ...)
val bestB: B = (for{
element <- aList
} yield element (c)).flatten.head // Because the List aList is ordered: AB1 is the best case, AB2 the second best and so on.
I do that for every List[C] out of the List[List[C]]. So in the end I may have a List[B] = (AB3, AB1, AB2, AB1) (for example). From this list I want to get the "best" Bs according to the order of aList at first - then i get List[B] = (AB1, AB1). These two AB1's I want to compare and then put the "better" Element (according to the compare function of the corresponding object) or both into a new list. This is want I want to accomplish.
To be honest, I'm still confused with what is your root problem. But I will try to answer what I understood.
First, if you want to change the type of the arguments when overriding a function from a trait. Then, the answer is you can't! - Because that would break the Liskov Substitution Principle.
But you can achieve what you want with a Type Class .
trait B {}
trait BComparator[Bi <: B] {
def compare(firstB: Bi, secondB: Bi): Int
}
Now if you want a way to dynamically obtain the right instance of the BComparator for your sub-classes you could make those instances as implicit values in their companion objects.
class B1 extends B {}
object B1 {
implicit val B1Comparator: BComparator[B1] = new BComparator[B1] {
override def compare(firstB: B1, secondB: B2): Int = ???
}
}
Now, given b11 and b12 as instances of B1 you could just write
implicitly[BComparator[B1]].compare(b11, b12)
I think you need to use implicit Comparator:
trait B
case class AB1(variable: Int) extends B
case class AB2(variable1: Int, variable2: Int) extends B
implicit object AB1Comparator extends Comparator[AB1] {
override def compare(o1: AB1, o2: AB1): Int = java.lang.Integer.compare(o1.variable, o2.variable)
}
implicit object AB2Comparator extends Comparator[AB2] {
override def compare(o1: AB2, o2: AB2): Int = java.lang.Integer.compare(o1.variable1, o2.variable1) match {
case 0 => java.lang.Integer.compare(o1.variable2, o2.variable2)
case other => other
}
}
def compare[A](obj1: A, obj2: A)(implicit comparator: Comparator[A]) = {
comparator.compare(obj1, obj2)
}
val ab1List = List(AB1(1), AB1(2), AB1(3))
val ab1Compare = compare(ab1List.head, ab1List.tail.head)
val ab2List = List(AB2(1, 1), AB2(1, 1), AB2(1, 3))
val ab2Compare = compare(ab2List.head, ab2List.tail.head)
Or if you want to sort list you should use Ordering:
trait B
case class AB1(variable: Int) extends B
implicit object AB1Ordering extends Ordering[AB1] {
override def compare(o1: AB1, o2: AB1): Int = java.lang.Integer.compare(o1.variable, o2.variable)
}
val ab1List = List(AB1(1), AB1(2), AB1(3))
val ab1ListSorted = ab1List.sorted
I need overriding implicit in object res defing one from trait. The purpose is to define custom implicits in one place (trait B). Trait a is defined in external library. Is it possible?
trait t {
}
object m extends t
object q extends t
trait a {
implicit val o: t = m
}
trait b {
implicit val o: t = q
}
trait c {
def action(implicit v: t): Unit = {}
}
object res extends c with a with b {
//i need smth like override val o = super[b].o
val ololo= action
}
It is not possible to mix-in two unrelated traits that both contain a member with the same identifier. The Scala compiler has no way to resolve which one would take precedence in this scenario. Since types A and B are related, o does not even need to have the same type in both of them. Trait B needs to extend A and override o. There isn't any other way to override a member without using inheritance.
trait T
case object M extends T
case object Q extends T
trait A {
implicit val o: T = M
}
trait B extends A {
override implicit val o: T = Q
}
trait C {
def action(implicit v: T): Unit = println(v)
}
object Res extends C with B {
def call() = action
}
scala> Res.call()
Q
Since you are already mixing A into Res anyway, it is assumed that A is not difficult to extend, since Res must implement any other unmentioned abstract members.
Suppose I've got an ADT like this:
sealed trait A extends Product with Serializable
object A {
case class A1() extends A
case class A2() extends A
case class A3() extends A
case class A4() extends A
}
Suppose also I have a trait AFoo like that:
type Foo = ...
trait AFoo { def asFoo(a: A): Foo }
Now I need to provide two different implementations for AFoo. So I am writing something like that:
abstract class AFooSupport extends AFoo {
protected def asFoo1(a1: A1): Foo
protected def asFoo2(a2: A2): Foo
protected def asFoo3(a3: A3): Foo
protected def asFoo4(a4: A4): Foo
def asFoo(a: A) = a match {
case a1: A1 => asFoo1(a1)
case a2: A2 => asFoo2(a2)
case a3: A3 => asFoo3(a3)
case a4: A4 => asFoo4(a4)
}
}
class AFoo1 extends AFooSupport {
// implement asFoo1, asFoo2, asFoo3, asFoo4
}
class AFoo2 extends AFooSupport {
// implement asFoo1, asFoo2, asFoo3, asFoo4
}
This approach will probably work but I wonder if there is a better way to do it. Would you use a type class in this case ?
There is only one function (A => Foo) for concrete classes with different implementations. I don't see here big advantage of using a type class. I would start considering a type class when one of arguments is generic.
As suggested in comments one could extract pattern matching into fold
def fold[F](a: A)(f1: A1 => F, ..., f4: A4 => F): F = a match {
case a1: A1 => f1(a1)
...
}
and implement required functions:
def aFoo(a: A): Foo = fold(a)(afoo1, afoo2, afoo3, afoo4)
def afoo1(a: A1): Foo = ...
...
def afoo4(a: A4): Foo = ...
def bFoo(a: A): Foo = fold(a)(bfoo1, bfoo2, bfoo3, bfoo4)
...
def bfoo4(a: A4): Foo = ...
But your AFooSupport is already sort of a fold implemented using inheritance instead of composition.
On the first look it seems like you could use generics as a Solution or solve your problem with inheritance.
I'm currently working on a A* implementation in Scala. To accomplish a clean structure I would like to use a nested case class structure which implements a self-bounded trait. However, I experience some issues when implementing this in the Scala IDE. The following code will not compile:
trait A[T <: A[T]]
class B {
case class C(int: Int) extends A[C] // A[this.C] won't work either
def make = C(5)
}
object D {
def foo[T <: A[T]](some: T) = {}
val c = new B().make
foo(c) // this does not compile
}
Is there any way I can make this structure work?
Not sure why you want this, but here's why it won't work as is:
The type of D.c is B#C. It is a path-dependent type where we don't know what instance of B it belongs to. However, C extends A[C], which is the already the same as saying A[this.C] in that context, which is bound to a specific instance of B. foo sees the type parameter T as B#C, which is not the same as b.C for some b.
You have two options to make this compile.
Relax the constraints of A to B#C:
trait A[T <: A[T]]
class B {
case class C(int: Int) extends A[B#C]
def make = C(5)
}
object D {
def foo[T <: A[T]](some: A[T]) = {}
val c = new B().make
foo(c)
}
Or handle the path-dependent type, so that c has type b.C:
trait A[T <: A[T]]
class B {
case class C(int: Int) extends A[C]
def make = C(5)
}
object D {
def foo[T <: A[T]](some: A[T]) = {}
val b = new B
val c: b.C = b.make
foo(c)
}