12

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?

2 Answers 2

12

You can use then block after initializing structs. It is described for classes in your first link in section Executing Side Effects in Constructors, but it works for structs as well.

[<Struct>]
type Foo = 
    val Name : string
    new(name : string) = { Name = name } 
                         then if name.StartsWith("A") then failwith "Haiz"

UPDATE:

Another way closer to your example is to use ; (sequential composition) and parentheses to combine expressions:

[<Struct>]
type Foo = 
    val Name : string
    new(name : string) = 
        { Name = ((if name.StartsWith("A") then failwith "Haiz"); name) } 
4
  • Ah, I'd tried using then within the { }. When that didn't work I assumed it was because it could only be used in a class constructor (as suggested in that section the linked article). When you say "not entirely the same", is that because the validation comes after the assignment?
    – Akash
    Sep 26, 2012 at 13:11
  • Yes, the object has been constructed before the exception is thrown.
    – pad
    Sep 26, 2012 at 13:15
  • I tested your example from C# by calling foo = new Foo("A") within a try/catch and then examining the foo instance within the catch block. I expected foo.Name to equal "A", but instead it was null i.e. the object had not been constructed. In which case your solution is equivalent to the C# code.
    – Akash
    Sep 26, 2012 at 13:38
  • Great, thank you for the info. I updated the answer accordingly.
    – pad
    Sep 26, 2012 at 13:46
7

If you want to avoid explicit fields (val) and then, two relatively esoteric features, you could use a static Create method and stick to the more common type definition syntax:

[<Struct>]
type Foo private (name: string) = 
  member x.Name = name
  static member Create(name: string) =
    Contract.Requires<ArgumentException> (name.StartsWith "A")
    Foo(name)
1
  • Good to know about that option - I didn't realise that you can define structures using the same syntax as classes (apart from the [<Struct>]). However, I'm not so keen on the Create method - I prefer a simple ctor so will stick with learning val and then.
    – Akash
    Sep 26, 2012 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.