我正在嘗試什么應該是一個非常基本的示例應用程式,但是來自 MS 的F# 介紹的這個特定示例在智能感知中產生了這個警告:
Type mismatch. Expecting a
''a -> int'
but given a
''a -> unit'
The type 'int' does not match the type 'uint'F# Compiler1
來自此代碼:(fwiw 我已經嘗試了sum 的隱式和顯式型別)
open System
[<EntryPoint>]
let main argv =
printfn "Welcome to the calculator program"
printfn "Type the first number"
let firstNo = Console.ReadLine()
printfn "Type the second number"
let secondNo = Console.ReadLine()
printfn "First %s, Second %s" firstNo secondNo
let sum: int = 0
printfn "The sum is %i" sum
然后,當我編譯時,根據警告,正如預期的那樣,我收到此錯誤:
error FS0001: Type mismatch. Expecting a? ''a -> int' ?but given a? ''a -> unit' ?The type 'int' does not match the type 'unit' [C:\Users\user-name\source\repos\MyFSharpApp\MyFSharpApp.fsproj]
作為 F# 的新手,我不確定為什么 printfn 函式認為 int 是一個單元并且破壞編譯。真的可以使用解釋和/或鏈接到檔案,說明為什么會出現這種情況以及如何解決它。
編輯
所以一個有趣的點,即使當我一起洗掉插值并只列印一個字串時,我也會得到同樣的錯誤。所以根本沒有 int,但字串需要一個 int 而不是一個單位......但是,為什么?我猜我有某種語法錯誤,盡管這看起來很奇怪。
uj5u.com熱心網友回復:
該main函式應該回傳一個int,它將成為程式執行結果。約定是:成功為零,非零表示錯誤代碼(這不是特定于 F# 的)。
當您使用 來創建新專案時dotnet new,正如練習所指示的那樣,main生成的函式會回傳零。
但是正如您在評論中確認的那樣,您的main函式當前沒有零,這可能意味著您不小心洗掉了它。
只需重新添加零,錯誤就會消失。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355965.html
