-1
什麼導致REPL打印函數簽名而不是函數結果?是什麼導致REPL打印函數簽名而不是函數結果?
我試圖執行以下行:
let email = Email "abc.com";;
email |> sendMessage |> ignore;;
的代碼如下
type PhoneNumber =
{ CountryCode:int
Number:string }
type ContactMethod =
| Email of string
| PhoneNumber of PhoneNumber
let sendMessage contact = function
| Email _ -> printf "Sending message via email"
| PhoneNumber phone -> printf "Sending message via phone"
// c. Create two values, one for the email address case and
// one for the phone number case, and pass them to sendMessage.
let email = Email "abc.com";;
email |> sendMessage |> ignore;;
我得到以下結果:
type PhoneNumber =
{CountryCode: int;
Number: string;}
type ContactMethod =
| Email of string
| PhoneNumber of PhoneNumber
val sendMessage : contact:'a -> _arg1:ContactMethod -> unit
val email : ContactMethod = Email "abc.com"
>
val it : unit =()
我希望這樣的事情:
「通過電子郵件發送消息」
經驗法則。如果你必須使用'ignore'並且不知道爲什麼,那麼仔細看一下函數簽名,可能性是你潛伏在代碼中的潛在錯誤。我發現調用.NET代碼的'ignore'比F#函數的用法更多,特別是當.NET代碼有副作用時'StringBuilder追加',你需要副作用但不是調用的結果。 –
另外,如果你曾經使用'按任意鍵繼續',那麼你會使用'Console.ReadKey()|> ignore';再次你需要副作用,但不是結果。 –