Scala无法解决复杂代码中的类型不匹配问题 [英] Scala failing to resolve a type mismatch problem in a complicated code
问题描述
我具有以下设置,并且我试图通过代码的编译类型检查,并且最好对其进行最小的修改,因为代码是由工具而不是手动生成的.
I have the following setup and I am trying to pass the compile type checking of the code with preferably minimal modification as the code is being generated by a tool and not by hand.
我认为问题是我需要为M_TEST_COLL
中的T_MAX_LATTICE[T]
或T_IntegerMaxLattice
提出一个更好的定义.
I think the problem is I need to come up with a better definition for T_MAX_LATTICE[T]
or T_IntegerMaxLattice
in M_TEST_COLL
.
代码有点大,所以我无法在此处发布整个代码,但我将回购URL放在了底部.我正在努力使类型层次结构可视化.
The code is kind of large so I can't post the whole code here but I put the repo URL at the bottom. I am struggling to visualize the type hierarchy.
我知道这个问题太笼统了,但是我要寻找的只是能够在不使用uncheck cast(或asInstanceOf
)的情况下编译代码
I know the question is too general but all I am looking for is being able to compile the code without using uncheck cast (or asInstanceOf
)
type T_MAX_LATTICE[T] = T;
trait C_TEST_COLL[T_Result, T_T] extends C_TYPE[T_Result] with C_TINY[T_Result] {
type T_IntegerMaxLattice;
val t_IntegerMaxLattice : C_TYPE[T_IntegerMaxLattice] with C_MAX_LATTICE[T_IntegerMaxLattice,T_Integer];
type T_Integers;
val t_Integers : C_TYPE[T_Integers]with C_SET[T_Integers,T_Integer];
class M_TEST_COLL[T_T](name : String,val t_T : C_TYPE[T_T] with C_TINY[T_T])
extends Module(name)
with C_TEST_COLL[T_T,T_T]
{
val t_Result : this.type = this;
val t_IntegerMaxLattice = new M_MAX_LATTICE[T_Integer]("IntegerMaxLattice",t_Integer,0);
type T_IntegerMaxLattice = T_MAX_LATTICE[T_Integer];
我得到的错误:
Error:Error:line (42)type mismatch;
found : M_MAX_LATTICE[basic_implicit.T_Integer]
(which expands to) M_MAX_LATTICE[Int]
required: C_TYPE[M_TEST_COLL.this.T_IntegerMaxLattice] with C_MAX_LATTICE[M_TEST_COLL.this.T_IntegerMaxLattice,basic_implicit.T_Integer]
(which expands to) C_TYPE[Int] with C_MAX_LATTICE[Int,Int]
val t_IntegerMaxLattice = new M_MAX_LATTICE[T_Integer]("IntegerMaxLattice",t_Integer,0);
推荐答案
我想我创建了最小的示例
I guess I created minimal example
type T_MAX_LATTICE[T] = T;
trait C_TEST_COLL[T_Result, T_T] extends C_TYPE[T_Result] with C_TINY[T_Result] {
type T_IntegerMaxLattice;
val t_IntegerMaxLattice: C_TYPE[T_IntegerMaxLattice] with C_MAX_LATTICE[T_IntegerMaxLattice, T_Integer];
type T_Integers;
val t_Integers: C_TYPE[T_Integers] with C_SET[T_Integers, T_Integer];
}
class M_TEST_COLL[T_T](name : String,val t_T : C_TYPE[T_T] with C_TINY[T_T])
extends Module(name)
with C_TEST_COLL[T_T,T_T] {
val t_Result: this.type = this;
val t_IntegerMaxLattice = new M_MAX_LATTICE[T_Integer]("IntegerMaxLattice", /*t_Integer,*/ 0);
type T_IntegerMaxLattice = T_MAX_LATTICE[T_Integer];
val t_Integers = ???/*new M_SET[T_Integer]("Integers",t_Integer);*/
type T_Integers /*= /*TI*/T_SET[T_Integer];*/
}
trait C_TYPE[T_Result] /*extends C_BASIC[T_Result] with C_PRINTABLE[T_Result]*/
trait C_TINY[T_Result] extends C_TYPE[T_Result]
trait C_MAX_LATTICE[T_Result, T_TO] /*extends C_MAKE_LATTICE[T_Result,T_TO]*/
type T_Integer = Int
// val t_Integer = new M_INTEGER("Integer")
trait C_SET[T_Result, T_ElemType] extends C_TYPE[T_Result] /*with C_COMPARABLE[T_Result] with C_COLLECTION[T_Result,T_ElemType] with C_ABSTRACT_SET[T_Result,T_ElemType] with C_COMBINABLE[T_Result]*/
class Module(val mname : String)
class M_MAX_LATTICE[T_TO]
(name : String, /*t_TO:C_ORDERED[T_TO],*/v_min_element : T_TO)
/*extends M_MAKE_LATTICE[T_TO](name,t_TO,v_min_element,
new M__basic_3[ T_TO](t_TO).v__op_z,
new M__basic_3[ T_TO](t_TO).v__op_z0,
new M__basic_13[ T_TO](t_TO).v_max,
new M__basic_13[ T_TO](t_TO).v_min)
with C_MAX_LATTICE[T_TO,T_TO] with C_ORDERED[T_TO]*/
我猜编译错误很明显.您尝试将类型为M_MAX_LATTICE[Int]
的new M_MAX_LATTICE[T_Integer]...
分配给t_IntegerMaxLattice
,以覆盖其他类型的值.
I guess compile error is clear. You try to assign new M_MAX_LATTICE[T_Integer]...
of type M_MAX_LATTICE[Int]
to t_IntegerMaxLattice
overriding a value of a different type.
如果您使类M_MAX_LATTICE
扩展特征C_TYPE
,则您的代码似乎可以编译
If you make class M_MAX_LATTICE
extend trait C_TYPE
your code seems to compile
class M_MAX_LATTICE[T_TO]
(name : String, t_TO:C_ORDERED[T_TO],v_min_element : T_TO)
extends M_MAKE_LATTICE[T_TO](name,t_TO,v_min_element,
new M__basic_3[ T_TO](t_TO).v__op_z,
new M__basic_3[ T_TO](t_TO).v__op_z0,
new M__basic_13[ T_TO](t_TO).v_max,
new M__basic_13[ T_TO](t_TO).v_min)
with C_MAX_LATTICE[T_TO,T_TO] with C_ORDERED[T_TO]
with C_TYPE[T_TO] //added
{
val v_less = t_TO.v_less;
val v_less_equal = t_TO.v_less_equal;
val v_assert: T_TO => Unit = ??? //added
val v_node_equivalent: (T_TO, T_TO) => T_OrLattice = ??? //added
val v_string: T_TO => String = ??? //added
}
这篇关于Scala无法解决复杂代码中的类型不匹配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!