0

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?

1 Answer 1

1

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)
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.