I am trying to convert a java webapp into scala. I been struggling with Type Mismatch errors which is not very helpful in determining cause of problem or I don't know how to read them. I have tried asking about issue in whole design in following post but looks like that's not helping me.
https://stackguides.com/questions/32573360/scala-type-mismatch-errors-when-using-type-classes-and-factory-methods
So I am breaking down my questions to small pieces. Hopefully that will lead to final solution. Can someone help me resolve contract between DataContext and DataContextBuilder?
Parts taken from above post:
trait DBObject
trait AggDBObject extends DBObject
trait RawDBObject extends DBObject
class Dimensions1Agg extends AggDBObject
class Dimensions2Agg extends AggDBObject
class Dimensions1Raw extends RawDBObject
trait IDataContext[A <: DBObject] {
var XsummaryData: A = _
var XByDateData : Map[String, A] = _
... more feilds ..
def restrictAccess = {.. some impl ..}
}
trait IDataContextBuilder[A <: DBObject] {
def initDataPoints(dataContext: IDataContext[A]): Unit
}
class Dimension1AggContextBuilder extends IDataContextBuilder[Dimension1Agg] {
.. override method impl..
}
object IDataContext {
def apply(sId: Int, mId: Int): DataContext[_ <: DBObject] = {
(sId, mId) match {
case (1, 1) => {
new DataContext[Dimension1Agg]()
}
case (1, 2) => {
new DataContext[Dimension1Raw]()
}
}
}
}
Do I have right return type of apply method for object I am creating? If I remove it then I get some complex return type at call site "val dataContext: DataContext[_ >: Dimension1Agg with Dimension1Raw <: DBObject] with Product with Serializable { .. }"
object IDataContextBuilder {
def apply(sId: Int, daId: Int): IDataContextBuilder[_ <: DBObject] = {
(sId, daId) match {
case (1, 1) => {
new Dimension1AggContextBuilder
}
case (1, 2) => {
new Dimension1RawContextBuilder
}
}
}
}
Do you see any issue in general with above factory methods?
I get following error when passing that DataContext to DataContextBuilder
type mismatch; found : IDataContext[_$1] where type $1 <: DBObject required: IDataContext[$19]
Is _$1 and _$19 above because scalac can't determine types?
EDIT:
Call site it a DataService class that receive user request params, orchestrate all above components and compose a response. I know its not functional but I am trying to do incremental refactoring. DataService check request params, passes that information to factory methods to create mutliple DataContext and DataContextBuilder and DataWorker; Call all the DataContextBuilder's initDataPoints methods, wait for them to finish; call all the Dataworker's generateView method, wait for them to finish, finally compose response.
initialize a map of each measure to DataContext var measureToDCMap = MapInt, IDataContext[_ <: DBObject]
for each measuer it performs following to populate DataContext
Get concrete DataContext from its factory method Get concrete DataContextBuilder from its factory method Call DataContextBuilder's initDataPoints method
all DataContext is populated with corosponding DimensionData at this point.
initialize dataView map for each view of each measure for each metrix(avg tweets per day, avg tweets per topic, tweet rate percentile etc)
var dataView = MapInt, Map[Int, Map[Int, List[DataView]]] 5) for each view it performs following to generate final viewable response
Get concrete ViewWorker . i.e. Metrix1ViewWorker or MetrixViewWorker
Call getData method
- generate final response
DataContextof the right type? - Daniel Langdon