Skip to content

Invalid location of comma when using named parameters for a class #2865

@MNie

Description

@MNie

Issue created from fantomas-online

Code

type MyCSharpClass() =
    member val Prop = 0.0m with get, set
    member val Prop2 = 0.0m with get, set

type FSharpType = {
    SomeValue: string option
}

let v = { SomeValue = Some "str" }

let instance = 
    MyClass(
        Prop = 
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
        ,
        Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m
    )

Result

type MyCSharpClass() =
    member val Prop = 0.0m with get, set
    member val Prop2 = 0.0m with get, set

type FSharpType = { SomeValue: string option }

let v = { SomeValue = Some "str" }

let instance =
    MyClass(
        Prop =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m,
        Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m
    )

Problem description

It seems that when we have multiple match statements passed to named parameters for a class it places the , in the wrong place. That results in a dotnet fantomas error.

Known workarounds are:

  1. surround the match statement with (),
let instance = 
    MyClass(
        (Prop = 
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m)
        ,
        (Prop2 =
            match value.SomeValue with
            | Some _ -> 0.0m
            | None -> 0.0m)
    )
  1. extract the match statement to an explicit variable.
let instance = 
    let v1 =
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
    let v2 =
            match value.SomeValue with 
            | Some _ -> 0.0m
            | None -> 0.0m
    MyClass(
        Prop = v1,
        Prop2 = v2
    )

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas main branch at 2023-04-20T16:04:28Z - 4ed1e9d

Default Fantomas configuration

  • Fantomas v6,
  • .NET6

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions