【发布时间】:2014-09-05 17:11:58
【问题描述】:
假设有一个类有一个公共构造函数,它接受一个参数。此外,我还想设置多个公共属性。 F# 中的语法是什么?例如在 C# 中
public class SomeClass
{
public string SomeProperty { get; set; }
public SomeClass(string s)
{ }
}
//And associated usage.
var sc = new SomeClass("") { SomeProperty = "" };
在 F# 中,我可以使用构造函数或 property setters 来完成此操作,但不能像在 C# 中那样同时使用两者。例如,以下内容无效
let sc1 = new SomeClass("", SomeProperty = "")
let sc2 = new SomeClass(s = "", SomeProperty = "")
let sc3 = new SomeClass("")(SomeProperty = "")
我好像遗漏了什么,但是什么?
let t = new TopicDescription("", IsReadOnly = true)
相应的编译器错误将是Method 'set_IsReadOnly' is not accessible from this code location。
【问题讨论】: