f# - 在 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# 语言规范中,您所追求的特定功能称为 Offside Context。这将允许您减少对最后一个上下文的列以及一个或多个附加列的缩进。越位上下文将在上下文中的=
赋值之后立即遇到Let
,以及在上下文中的开头括号之后Paren
。
实际上,您可以有这样的缩进:
let request =
Models.UserService.CreateUserRequest(
email = "email@example.com",
password = "password",
... )
推荐阅读
- java - TCP/IP服务器的spring boot处理
- laravel - 通过 Laravel 发送 FCM 消息以在我的 android 应用程序中打开深层链接
- telethon - 如何通过 ID 或名称检查用户?
- html - 如何仅针对特定表格更改图标而不是整体更改
- angular - TypeError:添加自定义验证器时无法读取未定义 Angular 的属性“indexOf”
- apache - 从 apache .htaccess 密码保护站点中排除目录中的文件
- python - 从父类调用 super
- java - 使用正则表达式删除字符串中的前导大写字符
- javascript - [i] 在这个函数中做了什么?
- kubernetes - 如果我更新一个秘密,它是否会自动更新到它被卷安装的所有资源?