我正在尝试创建一个复杂类型,并且需要在构建时执行一些操作。 于是我开始写我的代码:试图将一个用于或转让别的东西编译器发疯时了解F#类型的构造函数
type public MyType =
val private myvar: int
val private myvar2: string
(* ...Some val declarations... *)
new() = {
(* The default ctor makes something *)
}
new (ctorpar: AnotherType) = {
myvar = 1;
myvar2 = "Hello";
(* Other assignments in the form var = val *)
(* Here I would like to start a cycle in order *)
for i in ctorpar.array do
(* Do something *) (* ERROR *)
}
嘛。 我假设以下内容:新的语法遵循计算表达式之一,或更好,新是一个计算表达式(我暗示这是因为大括号和从一个指令到另一个指令的分号)。在这种情况下,对于构造函数计算表达式,仅可以分配赋值。
所以,请你能不能回答我:
1)是我扣是否正确? (关于计算表达式和类型的构造函数)。
2)我应该怎么做我是否需要放置铰接式指令集在构造函数中执行????那么你知道,有时候有必要在施工时执行一个动作,它可能涉及从一个循环到所有可能性的一切。
但是,编译生气反正...
感谢KVD我明白,我必须做以下可能性:
type public MyType =
val private myvar: int
val private myvar2: string
(* ...Some val declarations... *)
new() = {
(* The default ctor makes something *)
}
new (ctorpar: AnotherType) =
for i in ctorpar.ACollection do
(* Something *)
{
myvar = 1;
myvar2 = "Hello";
}
嗯,我很抱歉,但是这并不能帮助我因为F#编译器告诉我:
对象构造不能直接使用 尝试/使用,并尝试/对象的最终前 初始化。这个 包括构造函数,例如'for x in ...',可以详细说明这些构造的用法。这是Common IL强加的限制 。
OK,如果这个问题是在做之前对象initalization东西,这听起来是正确的,那么让我们做到这一点后:
的:
type public MyType = val mutable private myvar: int val mutable private myvar2: string (* ...Some val declarations... *) new() = { (* The default ctor makes something *) } new (ctorpar: AnotherType) = { myvar = 1; myvar2 = "Hello"; } then for i in ctorpar.ACollection do (* Something *) myvar <- 10
的故障再次受挫值或构造函数'myvar'是 未定义。
我应该怎么办???? 看起来,之后,它不识别我的类中的元素,它似乎是正确的,因为它需要一个标识符来声明使用self或this的成员时......在这里它没有自引用,并且正确地告诉我:“你想要得到我不能给你的东西!!!!!!”
kvb非常感谢你,你真的很清楚我的一切,不仅仅是我刚刚问过的,而是更多。现在它使感知。非常感谢你。 – Andry 2011-02-15 20:24:51
“then”关键字在文档中出现在哪里?它绝对有效,但我认为我以前从未见过它。 – 2011-02-15 20:37:14