I have a class that extends a Trait and I want to write Traits that can mix in with the class and override some of the methods.
The trait that my class is extending looks like this
trait CharSource {
def source: Iterator[Char]
def words: Iterator[String] = {
while(source.hasNext){
//Logic to get the next word from a Char Iterator
}
}
final def chars: Iterator[Char] = words.toString().toIterator
}
The class that extends CharSource
class IteratorDocumentSource(iterator: Iterator[Char]) extends CharSource {
def source = iterator
}
Now I want to write a trait to override the source def in IteratorDocutmentSource for some special behavior
trait PunctuationRemover extends Transformer {self: CharSource =>
override
/** Character source. Overriding should be in terms of `super.source` for
stackability. */
abstract val source: Iterator[Char] = {
super.source
//additional logic ...
}
}
The transformer trait that PunctioationRemover extends
trait Transformer {self: CharSource =>
protected def source: Iterator[Char]
def words: Iterator[String]
}
I get an error when making this call
new IteratorDocumentSource("Hello World!".iterator) with PunctuationRemover
Error:
An exception or error caused a run to abort:
SampleSuite$$anonfun$2$$anon$3.document$PunctuationRemover$$super$source()Lscala/collection/Iterator;
java.lang.AbstractMethodError:
SampleSuite$$anonfun$2$$anon$3.document$PunctuationRemover$$super$source()Lscala/collection/Iterator;
I referenced this post but I think my situation is a little different
Can I override a scala class method with a method from a trait?
Related
Scala Compiler giving error where trying to run following program.
Error:(19, 8) class Logging inherits conflicting members:
method log in trait TimestampLogger of type (msg: String)Unit and
method log in trait ShortLogger of type (msg: String)Unit
(Note: this can be resolved by declaring an override in class Logging.)
class Logging extends TimestampLogger with ShortLogger
^
trait ShortLogger {
def log (msg: String) {println(msg)}
trait TimestampLogger {
def log (msg: String) {
println("We are in Timestamp Logger")
println(new java.util.Date() )
}
}
class Logging extends TimestampLogger with ShortLogger
val a = new Logging
a.log("Log Message")
Why Stackable Modification in scala traits not suggesting the correct log method?
Why are we getting compilation error?
The error here is as message says lack of overrides.
You cannot have 2 different implementations of a method with the same signature. You have to resolve it. With traits you can have one implementation override another - this is actually required to perform trait linearization. But to be able to override you have to prove that overrides are about the same method. One way to fix it is to let methods override some base member. It always picks the "last" implementation.
trait Logger { def log(msg: String): Unit = () }
trait ShortLogger extends Logger {
override def log(msg: String) = println(msg)
}
trait TimestampLogger extends Logger {
override def log(msg: String): Unit = {
println("We are in Timestamp Logger")
println(new java.util.Date() )
}
}
// last override "wins" => ShortLogger implementation will be used
class Logging1 extends TimestampLogger with ShortLogger
// last override "wins" => TimestampLogger implementation will be used
class Logging2 extends ShortLogger with TimestampLogger
If you don't want to use last-wins, you have to reimplement the method in the place where they are conflicting:
class Logging extends TimestampLogger with ShortLogger {
override def log(msg: String) = ...
}
If you wanted to have both functionalities you would have to use a former approach + super
trait Logger { def log(msg: String): Unit = () }
trait ShortLogger extends Logger {
override def log(msg: String) = {
super.log(msg)
println(msg)
}
}
trait TimestampLogger extends Logger {
override def log(msg: String): Unit = {
super.log(msg)
println("We are in Timestamp Logger")
println(new java.util.Date())
}
}
// both will perform both loggings, but in a different order
class Logging1 extends TimestampLogger with ShortLogger
class Logging2 extends ShortLogger with TimestampLogger
I have the following trait in scala:
trait BaseDao {
type T <: BaseModel
def saveModel(model: T)
}
I created a class which extends BaseDao:
class Sample (dynamoDBMapper: DynamoDBMapper) extends BaseDao with LazyLogging {
checkNotNull(dynamoDBMapper)
protected def classType: Class[T] = implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]]
override def saveModel(model: T): Unit ={
model.validate()
dynamoDBMapper.save(model)
}
Now, when I create the instance of above class and try to call save method:
val testModel = new TestModel();
testModel.setHashKey("a")
testModel.setSortKey("b")
testModel.setCreatedOn(new Date())
testModel.setModifiedOn(new Date())
sample.saveModel(testModel)
It gives the following error:
Type mismatch expected: Sample#T, actual: TestModel model class extends BaseModel class
I am new in scala and I am not able to figure out what is wrong here?
You haven't defined type T in Sample.
Rewrite your Sample class to look somewhat like this:
class Sample (dynamoDBMapper: DynamoDBMapper) extends BaseDao with LazyLogging {
checkNotNull(dynamoDBMapper)
type T = TestModel
override def saveModel(model: T): Unit ={
model.validate()
dynamoDBMapper.save(model)
}
}
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)
}
There is the following API for Slick CRUD (Slick-2.1.0, Scala-2.11.4):
trait HasId {
type Id
def id: Option[Id]
}
trait HasIdColumn[E <: HasId] {
def id: scala.slick.lifted.Column[E#Id]
}
trait SlickExtensions {
val driver: scala.slick.driver.JdbcProfile
import driver.simple._
trait BaseDAO[T <: Table[E], E] {
val query: TableQuery[T]
}
trait HasIdActions[T <: Table[E] with HasIdColumn[E], E <: HasId]
extends BaseDAO[T, E] {
//line L1: this implicit val is needed to execute query.filter(_.id === id)
// what can be done in order to save the user from the necessity
// to override this value?
implicit val mappingId: BaseColumnType[E#Id]
def findById(id: E#Id)(implicit session: Session): Option[E] =
query.filter(_.id === id).firstOption
...
}
}
I apply this SlickExtensions as follows:
case class Entity(id: Option[Long] = None, value: String) extends HasId {
type Id = Long }
trait EntityComponent extends SlickExtensions {
import driver.simple._
class EntitiesTable(tag: Tag) extends Table[Entity](tag, "entities")
with HasIdColumn[Entity] {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def value = column[String]("value", O.NotNull)
def * = (id.?, value) <>(Entity.tupled, Entity.unapply)
}
object Entities extends HasIdActions[EntitiesTable, Entity] {
val query = TableQuery[EntitiesTable]
/* line L2: from slick library: ImplicitColumnTypes */
override implicit val mappingId = driver.simple.longColumnType
}
}
End point to execute queries:
val c = new EntityComponent {
lazy val driver = play.api.db.slick.Config.driver
}
db.withSession { implicit session =>
c.Entities.findById(1) foreach println
}
The main question is how to get rid of "implicit val mappingId" overriding in line L2?
I tried to create a class:
abstract class IdImplicits[E<:HasId](implicit val mappingId:BaseColumnType[E#Id])
and inherited it as follows:
object Entities extends IdImplicits[EntitiesTable, Entity]
with HasIdActions[EntitiesTable, Entity] {
val query = TableQuery[EntitiesTable]
//override implicit val mappingId: driver.simple.longColumnType
}
However it seems to me that such approach is redundant.
It would be great if I could hide "implicit val mappingId" inside SlickExtensions.
Here is the link to the same question
UPD:
In my project, I'd like to add HasName, HasValue[V] and some other mixins to construct the following DAOs:
object EntitiesDAO extends HasIdActions
with HasNameActions
with HasValueActions with NameToIdActions with ValueToIdActions {
...
override def nameToId(name:String):Option[E#Id]
override def valueToId(value:E#ValueType):Option[E#Id]
...
}
It leads to the following problems:
1) implicits for BaseColumnTypes, mentioned in my topic, should be taken into consideration for HasId, HasValue mixins
2) If implicits BaseColumnTypes are used as parameters of constructor of abstract classes then these classes can't be mixed in one EntityDAO object (the problem is described here).
3) If one abstract class is used for each variant of EntityDAO, then we get ugly-looking combinations, for example:
abstract class IdValueNameImplicits[E <: HasId with HasValue with HasName]
(implicit val idMapper:BaseColumnType[E#Id],
implicit val valueMapper:BaseColumnType[E#ValueType])
You can't do that because you are inside a trait and E#Id is only defined when you have a concrete implementation of it.
As you already discovered, you have to define your BaseColumnType when your trait is implemented because only then you have a defined type for E#Id.
Another option is not to have a trait but an abstract class where you can have a implicit BaseColumnType passed to the constructor.
I have a small project that does exactly what you are looking for. You can find it here: https://github.com/strongtyped/active-slick
There is also an Activator template.
http://typesafe.com/activator/template/slick-active-record
You can use it as is or as inspiration for your own.
Have fun!
I have a question concerning Scala override (as my title suggests)
Now I have the following classes/traits:
trait FSM {def transitionGraph:Map[String,(JsValue,FSM)]
abstract class AClass: FSM { def transitionGraph }
class Class extends AClass{ override def transitionGraph ... } <-- Wont work
trait OverrideTrait extends AClass { abstract override def transitionGraph } <-- works
class NewClass extends OverrideTrait { } <--- Works, I can use the overridden transitionGraph
My question is: Why can I not override things from an abstract class. Is it because I am never allowed to instantiate an abstract class. Thus the behavior :
val AClass class = new Class
is never allowed to happen?
Thanks.
There seems to be a lot of stuff omitted from the code you've given, so I'm not sure I get the question, but here's something similar that does compile:
trait FSM { def transitionGraph: String }
abstract class AClass extends FSM { def transitionGraph: String }
class Class extends AClass { override def transitionGraph = ??? }
trait OverrideTrait extends AClass { override def transitionGraph = ??? }
class NewClass extends OverrideTrait { }
Does this help at all?
Your code example wouldn't compile. But it should work once you corrected a few things:
trait FSM {def transitionGraph:Map[String,(JsValue,FSM)]}
abstract class AbstractClass extends FSM { def transitionGraph }
class ConcreteClass extends AbstractClass{ def transitionGraph = ??? }
val someClass: AbstractClass = new ConcreteClass