Here is a trivial C# struct that does some validation on the ctor argument:
public struct Foo
{
public string Name { get; private set; }
public Foo(string name)
: this()
{
Contract.Requires<ArgumentException>(name.StartsWith("A"));
Name = name;
}
}
I've managed to translate this into an F# class:
type Foo(name : string) =
do
Contract.Requires<ArgumentException> (name.StartsWith "A")
member x.Name = name
However, I can't translate this to a structure in F#:
[<Struct>]
type Foo =
val Name : string
new(name : string) = { do Contract.Requires<ArgumentException> (name.StartsWith "A"); Name = name }
This gives compile errors:
Invalid record, sequence or computation expression. Sequence expressions should be of the form 'seq { ... }'
This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor.
I've had a look at this and this but they do not cover argument validation.
Where am I doing wrong?