为什么可以忽略ICommand.Execute方法的参数?

时间:2016-01-09 18:28:57

标签: f#

为什么以下语句不需要参数?

viewModel.Submit.Execute()

参数是隐式单位类型吗?

ICommand.Execute的签名如下:

/// <summary>
///     Defines the method that should be executed when the command is executed.
/// </summary>
/// <param name="parameter">A parameter that may be used in executing the command. This parameter may be ignored by some implementations.</param>
void Execute(object parameter);

请注意摘要的最后评论:

  

某些实现可能会忽略此参数。

以下DelegateCommand实现如下:

module UILogic.Interaction

open System
open System.Windows
open System.Windows.Input
open System.ComponentModel

type DelegateCommand (action:(obj -> unit), canExecute:(obj -> bool)) =
    let event = new DelegateEvent<EventHandler>()
    interface ICommand with
        [<CLIEvent>]
        member this.CanExecuteChanged = event.Publish
        member this.CanExecute arg = canExecute(arg)
        member this.Execute arg = action(arg)

此命令的客户端如下:

// Setup
let viewModel = ViewModel()
viewModel.FirstName <- "Scott"
viewModel.LastName <- "Nimrod"

// Test
viewModel.Submit.Execute()

总之,我只是不明白如何在F#中忽略未标记为可选的参数。

有关为何会出现这种情况的任何解释?

1 个答案:

答案 0 :(得分:3)

()是F#中的unit类型的值} ...它将被翻译为null

如果您愿意,可以尝试:

> ();;
val it : unit = ()
> box ();;
val it : obj
= null