在F#中使用私有构造函数创建一个类

Ove*_*ive 22 f# constructor private class

我想创建一个只有一个私有构造函数的类.现在我知道如何在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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

rig*_*old 39

主构造函数的访问说明符遵循类型的名称和类型参数:

type ValidString private (value: string) =
    ...
Run Code Online (Sandbox Code Playgroud)