我正在查看我在培训课程中看到的一个函数,并且无法理解在以下函数末尾使用"()":
let acclock (start:DateTimeOffset) rate () = let now = DateTime.Now let elapsed = now - start start.AddTicks (elapsed.Ticks * rate)
为什么函数签名在其签名的其他参数的末尾有一个单位参数?
因此,我认为单位参数意味着没有参数或返回类型,类似于"void".
这与部分应用有关.您可以将此函数绑定到另一个名称,同时提供start
和rate
参数,创建类型的函数() -> DateTime
.只有在调用该函数时,才会执行"elapsed = now - start"和"start.AddTicks"的计算.像这样:
let timeThis = let stopClock = acclock DateTime.Now 500 doStuff () stopClock () // The function is only executed now
如果最后没有()
参数,则在添加rate
值时直接执行该语句.
let acclock' (start:DateTimeOffset) rate = let now = DateTime.Now let elapsed = now - start start.AddTicks (elapsed.Ticks * rate) let timeThis1 = let stopClock = acclock' DateTime.Now doStuff () stopClock 500 // This is only executed now // Or let timeThis2 = let stopClock = acclock' DateTime.Now 500 // Wait, we calculated it immediately doStuff () stopClock // This is just a value