gpt4 book ai didi

f# - 在 F# 中,为什么空格会影响此代码

转载 作者:行者123 更新时间:2023-12-05 03:25:21 26 4
gpt4 key购买 nike

我有这种类型:

type ClockEvent (name: string, interval: TimeSpan, callback: unit -> unit) =
...
member this.Start () =
...

我这样使用它:

ClockEvent("autowrite", TimeSpan.FromSeconds(0.5), fun _ ->
match databaseWriterAsync connectionString |> Async.RunSynchronously with
| Ok _ -> ()
| Error e -> error $"couldn't write to the candles and kwap to the database: {e.Describe()}"
).Start()

按预期工作。

但是如果我在“ClockEvent”和“(”之间添加一个空格,我会得到:

ClockEvent ("autowrite", TimeSpan.FromSeconds(0.5), fun _ ->
match databaseWriterAsync connectionString |> Async.RunSynchronously with
| Ok _ -> ()
| Error e -> error $"couldn't write to the candles and kwap to the database: {e.Describe()}"
).Start()

然后,突然:

Writer.fs(54, 20): [FS0597] Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized

我知道问题与末尾的“.Start()”有关,但我不明白空格是如何导致编译器以不同方式解释代码的。

最佳答案

要添加更多上下文,棘手的是 F# 中函数应用程序和方法调用之间存在细微差别。

原则上,带空格和不带空格都可以写:

foo(14)  // Function call, but odd syntax with parentheses
foo (14) // The same, but now without space
foo 14 // This is how you typically write it

与方法调用相同,但这里的约定不同:

"abc".IndexOf('b') // Normal way of writing it
"abc".IndexOf 'b' // You are allowed to have a space
"abc".IndexOf 'b' // Parentheses around the 'a' expression

空格的问题是 F# 通常要求您用括号括起来:

foo (bar 1 (qux 2 3))

如果您必须通过方法调用来执行此操作(在 F# 的旧版本中就是这种情况),那将非常乏味:

("foo".Substring(1)).Equals("oo") // This looks ugly
("foo".Substring 1).Equals "oo" // But you can have spaces

所以,特殊的高优先级应用规则说,如果你没有空格(看起来像一个方法调用),你不需要写括号:

"foo".Substring(1).Equals("oo") // Works thanks to high-precedence rule

关于f# - 在 F# 中,为什么空格会影响此代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72028786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com