我想做一个有 的类(class)仅限 一个私有(private)的构造函数。现在我知道如何在 C# 中做到这一点,但我不知道如何在 F# 中做到这一点。
基本上这就是我想在 F# 中做的事情:
/// <summary>
/// A valid string is a string of 50 characters or less that contains only letters of the latin alphabet.
/// </summary>
public sealed class ValidString {
private readonly string _value;
private ValidString(string value) {
_value = value;
}
public string Value { get { return _value; } }
public static bool TryParse(string input, out ValidString validStr) {
bool valid = input.Length <= 50 && input.All(Char.IsLetter);
validStr = valid ? new ValidString(input) : null;
return valid;
}
}
最佳答案
主构造函数的访问说明符遵循类型的名称和类型参数:
type ValidString private (value: string) =
...
关于class - 在 F# 中使用私有(private)构造函数创建一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450581/