首页 > 解决方案 > 在 F# 中使用多行命名参数调用方法

问题描述

我有一些 F# 代码调用用 C# 编写的带有很多参数的方法。

为了使事情清晰易读,我在调用方法时使用了命名参数:

let request = Models.UserService.CreateUserRequest(
    email = "email@example.com",
    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"
)

但是我收到有关缩进的警告:

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.

显然,如果我不使用命名参数或将所有内容放在一行中,警告就会消失。但我想以一种清晰易读的方式格式化它。

有没有办法在 F# 中格式化多行的方法调用而不会发出警告?

标签: f#c#-to-f#

解决方案


轻量级语法的正式描述包含在F# 语言规范中,您所追求的特定功能称为 Offside Context。这将允许您减少对最后一个上下文的列以及一个或多个附加列的缩进。越位上下文将在上下文中的=赋值之后立即遇到Let,以及在上下文中的开头括号之后Paren

实际上,您可以有这样的缩进:

let request =
    Models.UserService.CreateUserRequest(
        email = "email@example.com",
        password = "password",
        ... )

推荐阅读