Skip to content

Improve error reporting: Missing , from object initializer #1218

@isaacabraham

Description

@isaacabraham

What

The following code leads to the resulting misleading error message: -

type Person() =
    member val Name = "" with get,set
    member val Age = 0 with get,set


let p =
    Person(Name = "Fred"
           Age = 18)

error FS0501: The member or object constructor 'Person' takes 0 argument(s) but is here given 1. The required signature is 'new : unit -> Person'

Why

The error above gets confused - it firstly assumes that we're trying to call a constructor with arguments when in fact we're trying to set properties on the object. Also it suggests 1 argument is provided but as far as the developer is concerned they have provided 2. The last point is that the error has nothing to do with this - it's simply that we've missed out the , between the two property assignments.

How

I'm not entirely sure what the real syntax error should be - let's start by my suggesting rewording the error to something like this: -

Syntax error. Were you trying to set the value of multiple properties on the object with object initializer syntax? If so, consider separating arguments with a ,.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions