我不确定问题是什么或运行时错误试图告诉我的。我有一个类在类的构造过程中尝试分配一个对象,但是,我得到以下运行时错误:
格式错误的类型:第150行pos 26:在此上下文中使用'type_tree'无效 _output_type = new ** t ype_tree.asUnknown(); **
所以我在课堂上添加了一个测试方法,认为我在构造过程中无法分配:
class X {
X() {
type_tree doh = new type_tree.asUnknown(); <--- runtime error
}
void bogus() {
type_tree output_type = new type_tree.asUnknown();
}
}
...
X x = new X();
x.bogus(); <-- runtime error as well
然而,我可以在类之外分配 type_tree 对象:
type_tree obj = new type_tree.asUnknown(); <-- works
我之前从未遇到过这种类型的运行时错误,也无法弄清问题是什么。关于要寻找什么的任何想法?
以下是代码的更详细版本。
type_tree.dart
part of OpenCog.moses;
class type_tree extends NTree<type_node> {
// Basic named constructors
type_tree.asUnknown() {...}
type_tree.asIllFormed() {...}
}
procedure_call_base.dart
part of OpenCog.moses;
class ProcedureCallBase extends operator_base {
type_tree _output_type;
factory ProcedureCallBase.from(...) {
ProcedureCallBase(String name, int arity, combo_tree tr,
[bool infer_type = false]) : super(name),
_arity = arity,
_body = tr {
_output_type = new type_tree.asUnknown(); <-- runtime error
}
...
ProcedureCallBase v = new ProcedureCallBase(name, arity, tr, infer_type);
return v;
}
// A test method called in unit test.
void bogus() {
type_tree output_type = new type_tree.asUnknown(); <-- also fails at runtime
}
}
单元测试代码:
test("Check valid definition", () {
String proc = r"add(2) := +($1 $2)";
ProcedureCallBase v = new ProcedureCallBase.from(proc); <-- fails in the constructor
v.bogus(); <-- fails inside bogus method
});
test("Check type tree", () {
type_tree doh = new type_tree.asUnknown(); <-- this works.
});
基本上由于某种原因,我无法在 ProcedureCallBase 的上下文中创建一个type_tree对象,我不知道为什么。
Dart build:1.21.0-dev.11.2
答案 0 :(得分:0)
这可能是名字冲突。我已经看到分析仪在所有情况下都没有捕捉到它们。