Can an object infer an overridden field type from a characteristic?

I generate some test data as follows:

trait Template { val field1: Map[List[String], Map[Int, List[Boolean]]] //the type is for illustration. Think of it as a 'monstrous type definition' val field2: Map[List[Int], Map[Double, List[Option[String]]]] } object Fixture extends Template { override val field1 = Map() override val field2 = Map() } 

This fails with the message value field1 has incompatible type , but for field2 ?

I can fix this by explicitly specifying the types in Fixture , but I try to avoid this because if I change the type inside the attribute, I will need to propagate it to every fixture object.

Can my object Fixture field1 types for field1 and field2 from trait Template .

+7
scala
source share
1 answer

I don’t know why the compiler is not smart enough to infer the correct type for val , but it will be for def . You can work around the problem by setting the initialization code to def.

 trait Template { val field1 = field1Init def field1Init: Map[List[String], Map[Int, List[Boolean]]] val field2 = field2Init def field2Init: Map[List[Int], Map[Double, List[Option[String]]]] } object Fixture extends Template { override def field1Init = Map() override def field2Init = Map() } 
+3
source share

All Articles