Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling method in F# with named parameters on multiple lines

Tags:

f#

c#-to-f#

I have some F# code that calls a method written in C# with lots of parameters.

In order to make things legible I use named parameters when calling the method:

let request = Models.UserService.CreateUserRequest(
    email = "[email protected]",
    password = "password",
    title = "title",
    firstname = "firstname",
    lastname = "lastname",
    nickname = "nickname",
    locale = "locale",
    birthDate = "birthdate",
    currency = "USD",
    ipAddress = "127.0.0.1",
    address = address,
    mobilePhone = "+1 123 456 7890"
)

However I get a warning about the indentation:

warning FS0058: Possible incorrect indentation: this token is offside of context started at position (30:19). Try indenting this token further or using standard formatting conventions.

Obviously if I do not use named parameters or put everything in a single line, the warning disappears. But I want to format this in a way that is legible.

Is there a way to format a method call on multiple lines in F# without having a warning?

like image 772
Aymeric Barthe Avatar asked Sep 01 '25 02:09

Aymeric Barthe


1 Answers

The formal description of Lightweight Syntax is contained in the F# language spec, and the particular feature you are after is called Offside Context. This will allow you to reduce indentation to the column of the last context plus one or more additional columns. The Offside Context will be encountered immediately after the = assignment in a Let context, as well as after the opening paren in a Paren context.

In effect, you can have an indentation like this:

let request =
    Models.UserService.CreateUserRequest(
        email = "[email protected]",
        password = "password",
        ... )
like image 60
kaefer Avatar answered Sep 02 '25 21:09

kaefer