I'm trying to refactor an Enumeration to a sealed trait with concrete classes because I need to pack more functionality into them. With the sealed trait, I'd like to have functionality similar to Enumeration's withName(String) method. I've come up with the following code to do so and am looking for feedback:
sealed trait Foo {
def name: String
}
object Foo {
case object FooA extends Foo {
override val name: String = "a"
}
case object FooB extends Foo {
override val name: String = "b"
}
val values = Seq(FooA, FooB)
def withName(name: String): Option[Foo] = {
values.find(value => value.name.equals(name))
}
}
I can then use the withName(String) method to get the corresponding concrete object of type Foo as an Option:
val testFooAName = "a"
val testFooA = Foo.withName(testFooAName) // Will yield Some(FooA)
testFooA match {
case Some(Foo.FooA) => println("Matched Foo.FooA!")
case Some(Foo.FooB) => print("Matched Foo.FooB!")
}
val testFooNoneName = "none"
val testFooNone = Foo.withName(testFooNoneName) // Will yield None
Output:
Matched Foo.FooA!
Is this approach correct?
Yes, it looks fine
Minor simplification: Have a map for fast lookup
val values = Seq(FooA, FooB)
val fastLookup = values.map(v => v.name -> v).toMap
def withName(name: String): Option[Foo] = fastLookup.get(name)
There is a handy library by beachape which can be used to provide some of the functionality of enums to your sealed trait
the package you need to include in your sbt is:
"com.beachape" %% "enumeratum" % "1.5.15"
then extend your your object with the Enum like so:
import enumeratum._
sealed trait Foo
object Foo extends Enum[Foo] {
case object FooA extends Foo
case object FooB extends Foo
}
The you can use the withName function (amongst others) to get the sealed trait you want:
Foo.withName("FooA")
Related
I need to define a bunch of similar enums:
object Color extends MyEnum[Color.Color] {
type Color = Value
val Red, Green, Periwinkle = Value
}
object Shape extends MyEnum[Shape.Shape] {
type Shape = Value
val Square, Circle, Balbis = Value
}
I'd like to extract shared functionality into an abstract class or trait, to support 1) apply and unapply methods, and 2) implicit conversions from enums to their string representations:
abstract class MyEnum[T] extends Enumeration {
implicit def valueToString(value: T): String = value.toString
implicit def stringToValue(string: String): T = apply(string)
def unapply(arg: String): Option[T] =
values.find(_.toString == arg).map(_.asInstanceOf[T])
def apply(arg: String): T =
values.find(_.toString == arg)
.getOrElse(sys.error(s"Invalid value '$arg'"))
.asInstanceOf[T]
}
This doesn't compile; when I add the type param in MyEnum[Color.Color], it breaks the Value type for some reason.
Is there some way to get what I want with this enum syntax, or do I need to switch to a case object solution (or something more complicated)?
This Blog discusses the Enumeration Hell in Scala: scala-enumerations-hell
The solution is not to use Scala's Enumeration type but create your own Enumeration class. Here is the abstract class from that blog:
abstract class Enum[Enum : ClassTag] { self =>
val values = {
import runtime.universe._
val mirror = runtimeMirror(self.getClass.getClassLoader)
val classSymbol = mirror.classSymbol(self.getClass)
classSymbol.toType.members
.filter(_.isModule)
.map(symbol => mirror.reflectModule(symbol.asModule).instance)
.collect { case v: Enum => v }
}
def forName(name: String): Option[Enum] = values.find(_.name == name)
implicit class RichardEnum(enum: Enum) {
def name: String = enum.toString
}
}
There is also a framework that provides that for you: lloydmeta/enumeratum
Considering the following code :
sealed trait Foo {
def name: String
}
case object FooA extends Foo {
override val name: String = "a"
}
case object FooB extends Foo {
override val name: String = "b"
}
object Foo {
def fromString(name: String): Foo = {
name match {
case FooA.name => FooA
case FooB.name => FooB
}
}
Can I refactor the fromString() method to avoid having a case per case object instance ? Some more generic code able to enumerate through all Foo instances ?
In my real version, I start to have a lot of case object, and the wall of case bla.name => bla is boring me ^^
Thanks :)
Beachape provide an enum library which can do this for you out of the box:
include this in your build.sbt
"com.beachape" %% "enumeratum" % "1.5.15"
The Enum class provided just needs to be extended like so:
import enumeratum._
sealed trait Foo
object Foo extends Enum[Foo] {
case object FooA extends Foo
case object FooB extends Foo
}
There is a function called withName that will allow you to get the right sealed trait via its string name:
Foo.withName("FooA")
How about something like this?
sealed trait Foo {
def name: String
}
object Foo {
case object FooA extends Foo {
override val name: String = "a"
}
case object FooB extends Foo {
override val name: String = "b"
}
val values = Seq(FooA, FooB)
def withName(name: String): Option[Foo] = {
values.find(value => value.name.equals(name))
}
}
You can then use the withName(String) method to get the corresponding concrete object of type Foo as an Option:
val testFooAName = "a"
val testFooA = Foo.withName(testFooAName) // Will yield Some(FooA)
testFooA match {
case Some(Foo.FooA) => println("Matched Foo.FooA!")
case Some(Foo.FooB) => println("Matched Foo.FooB!")
}
val testFooNoneName = "none"
val testFooNone = Foo.withName(testFooNoneName) // Will yield None
Output:
Matched Foo.FooA!
Let's say I have case classes something like below.
trait Foo {
def a: String
}
case class Bar(a: String,b: Option[Int]) extends Foo{
def this(test: Test) = this(test.foo,None)
}
case class Buzz(a: String,b: Boolean) extends Foo{
def this(test: Test) = this(test.foo,false)
}
I'm using the constructor def this(test: Test) via reflection and working as I expected.
A method signature that I use the constructor is something like this
def test[T <: Foo: ClassTag](cb: (String) => Future[T]): Future[Result]
What I want to do is restrict that any case classes that extends trait Foo needs to have def this(test: Test).And the case if any of them don't have it, It should be a compile error.
My attempt
//Compile error
trait Foo[T] {
def a: String
def this(test: Test):T
}
Is there any way to do this?
Thanks in advance.
It is not possible to use the type system to enforce that a class has a specific constructor. This shouldn't really be a surprise, because you're already using reflection to access said constructor. Using a reflective call, the only way to check for the appropriate constructor would be to use more reflection--preferably via a macro to mail compilation fail.
There is almost always a better way than using reflection, though. In this case, we can use a type class to find the correct method that can build a sub-type of Foo (or anything, really) from a Test.
Let's assume Test looks like this:
case class Test(foo: String)
Then, we define a TestBuilder type class, which can provide evidence that we can build an A from a Test.
trait TestBuilder[A] {
def build(test: Test): A
}
// Convenience method for creating type class instances
object TestBuilder {
def apply[A](f: Test => A): TestBuilder[A] = new TestBuilder[A] {
def build(test: Test): A = f(test)
}
}
Then, we define out Foos, each with an instance of TestBuilder[A], where A is the type of each Foo:
trait Foo {
def a: String
}
case class Bar(a: String, b: Option[Int]) extends Foo
object Bar {
implicit val builder = TestBuilder(test => Bar(test.foo, None))
}
case class Buzz(a: String, b: Boolean) extends Foo
object Buzz {
implicit val builder = TestBuilder(test => Buzz(test.foo, false))
}
Note that we no longer need the alternate constructors, and rely on the type class instances to build our Foos using apply.
Now, your test method could look something like this. I changed around the return types because you don't define any implementation or what Result is, but the idea is the same.
def test[T <: Foo : ClassTag : TestBuilder](cb: String => Future[T]): Future[T] = {
val test = Test("abc")
// use the implicitly resolved type class to build `T` from a `Test`
val t = implicitly[TestBuilder[T]].build(test)
Future(t).andThen {
case Success(x) => cb(x.a)
}
}
Now, something like this will compile:
// T is Bar
scala> test((s: String) => Future(Bar(s, None)))
res0: scala.concurrent.Future[Bar] = scala.concurrent.impl.Promise$DefaultPromise#56f2bbea
And using some other type Baz, without an instance of TestBuilder[Baz] will fail.
case class Baz(a: String) extends Foo
scala> test((s: String) => Future(Baz(s)))
<console>:29: error: could not find implicit value for evidence parameter of type TestBuilder[Baz]
test((s: String) => Future(Baz(s)))
^
I don't think you can do quite what you're looking for. But maybe this would work for you:
trait Foo {
def a: String
def create(a: String): Foo
}
case class Bar(a: String,b: Option[Int]) extends Foo{
def create(a: String) = Bar(a,None)
}
case class Buzz(a: String,b: Boolean) extends Foo{
def create(a: String) = Buzz(a,false)
}
You would then have a way to construct a Bar or Buzz without having to specify the second parameter.
BTW, I didn't quite follow your template exactly because I didn't know what Test was supposed to be.
I don't think there's a way to do this directly. But one usually constructs case classes through factories -- their companion objects -- and that gives you the flexibility to do what you want in a different way.
Define
trait Test {
def foo : String = ???
}
abstract class Foo[T <: Foo[T]]()(implicit ev : FooMaker[T]) {
def a: String
}
trait FooMaker[T <: Foo[T]] {
def apply( test : Test ) : T
}
implicit object Bar extends FooMaker[Bar] {
def apply(test: Test) = Bar(test.foo,None)
}
case class Bar(a: String,b: Option[Int]) extends Foo[Bar]
implicit object Buzz extends FooMaker[Buzz] {
def apply(test: Test) = Buzz(test.foo,false)
}
case class Buzz(a: String,b: Boolean) extends Foo[Buzz]
But if you try to define a Foo without the factory method in the companion object that you require:
case class Barf(a : String, b : Short ) extends Foo[Barf]
You'll see
scala> case class Barf(a : String, b : Short ) extends Foo[Barf]
<console>:12: error: could not find implicit value for parameter ev: FooMaker[Barf]
case class Barf(a : String, b : Short ) extends Foo[Barf]
Add the companion object with the factory you need, and it's all good
implicit object Barf extends FooMaker[Barf] {
def apply(test: Test) = Barf(test.foo,0.toShort)
}
case class Barf(a : String, b : Short ) extends Foo[Barf]
In the REPL:
scala> :paste
// Entering paste mode (ctrl-D to finish)
implicit object Barf extends FooMaker[Barf] {
def apply(test: Test) = Barf(test.foo,0.toShort)
}
case class Barf(a : String, b : Short ) extends Foo[Barf]
// Exiting paste mode, now interpreting.
defined object Barf
defined class Barf
Note that to compile this stuff in the REPL you'll need to use :paste because the mutually interdependent definitions can't be defined separately.
I would like to have a sealed trait which have a declared method that returns the
actual class that extends the trait. Should I use an abstract type, a parameter type or
is there any other nice way to solve this?
sealed trait Foo {
type T
def doit(other: T): T
}
or
sealed trait Foo[T] {
def doit(other: T): T
}
Note that T must be a subtype of Foo in this example. If I do it like this the type
information feels too repeated:
case class Bar(name: String) extends Foo[Bar] {
def doit(other: Bar): Bar = ...
}
They are mostly interchangeable. According to Odersky, the reason was mainly for completeness: That similarly to the fact that methods and fields (values) can be either abstract or passed as parameters, so can types.
It is better to use an abstract type when you intend to mix several traits that all use the same type name. With type parameters you need to explicitly pass the type to each
Here's an article explaining all of this: http://www.artima.com/weblogs/viewpost.jsp?thread=270195
You can cut down on the repetition somewhat by having your doit method return a factory function:
trait Foo[T] {
self: T =>
def doit: T => T
}
case class Bar(name: String) extends Foo[Bar] {
// note: types omitted
def doit = { other => Bar(name + other.name) }
}
It's not possible to do the same with an abstract type:
trait Foo {
self: T => // won't compile because T isn't defined yet
type T
def doit: T => T
}
You can write:
trait Foo[T] {
self:T =>
def doit(other: T): T
}
case class Bar(name: String) extends Foo[Bar] {
def doit(other: Bar): Bar = ...
}
The difference to your example is that Bar can't be instantiated in any other way (e.g. case class Bar(name: String) extends Foo[String]).
trait Foo[A <: Foo[A]]
This trait can only be mixed in if A is a subtype of Foo[A] and the only type satisfying that is the class Foo is being mixed into. I saw this solution in the Mapper traits in Lift.
EDIT - Below is my original answer. Your comment indicates that you wish to return an arbitrary instance of a matching type but I don't really believe that this is in any way sensible. Suppose it were, via the T.type syntax:
trait T { def foo : T.type }
trait U extends T { def foo = new U } //must be a U
class W extends U
val w : W = (new W).foo //oh dear.
This is accomplishable via this.type:
scala> trait T {
| def foo : this.type
| }
defined trait T
scala> class W extends T {
| def foo = this
| }
defined class W
scala> (new W).foo
res0: W = W#22652552
scala> res0.foo
res1: res0.type = W#22652552
And then also:
scala> ((new W) : T)
res4: T = W#45ea414e
scala> res4.foo.foo.foo
res5: res4.type = W#45ea414e
I got some difficulties designing my case classes. A simplified version looks like:
abstract class Base(s: Option[String]) {
//code
}
case class CaseClass(s: Option[String] = None) extends Base(s) {
//code
}
And I have a method where I want to do something like:
def method(base : Base) = {
//code
base copy (s = Some("string"))
}
Of course I get:
value copy is not a member of Base
So what I want to do is create a new instance based on my base class (which is not a case class). Obviously one can not do this. But how would you solve this in a elegant way?
Thanks in advance!
If you parameterize your base class and also define the abstract copy method there, you can have the subclasses return instances of their own types from the copy method. In this case, you want CaseClass to return a CaseClass, presumably.
abstract class Base[T](s: Option[String]) {
def copy(in: Option[String]) : T
}
case class CaseClass(s: Option[String]) extends Base[CaseClass](s) {
def copy(in: Option[String]) = CaseClass(in)
}
case class OtherClass(s: Option[String]) extends Base[OtherClass](s) {
def copy(in: Option[String]) = OtherClass(in)
}
def method[T <: Base[T]](base: T) : T = {
base.copy(Some("String"))
}
scala> method(CaseClass(None))
res1: CaseClass = CaseClass(Some(String))
scala> method(OtherClass(Some("hi")))
res2: OtherClass = OtherClass(Some(String))
Other subclasses of Base would return their own types. The type parameter on #method is defined with an upper bound of Base[T]. This means that T must be any sub-type of Base[T] and is what allows you to supply instances of CaseClass and OtherClass as parameters to that method.
The behavior you're trying to achieve is not implementable. copy method of a case class is autogenerated by the compiler, and once you add a method called copy to your implementation, compiler will not generate any sugar.
You can reimplement copy with traits, but it will not be as flexible as the generated one (you will have to update the base trait, copy and method implementations every time the field-set of a case class changes):
sealed trait Base[T] {
val s: Option[String]
def copy(s: Option[String]) : T
}
case class CaseClass(override val s: Option[String] = None) extends Base[CaseClass] {
override def copy(s: Option[String]) = CaseClass(s)
}
def method[T <: Base[T]](base : Base[T]) = base copy (s = Some("strng"))
Alternatively, you can implement method as follows:
case class CaseClass(s: Option[String] = None)
def method[X <: {def copy(s: Option[String]):X}](base : X) =
base copy(s = Some("string"))
scala> method(CaseClass())
res4: CaseClass = CaseClass(Some(string))
Thus you won't need Base trait, and reduce the number of alterations, if your case classes change.