args[] in Main for F#

  • 本文关键字:for Main in args f#
  • 更新时间 :
  • 英文 :


以下是我在Chris Smith的《编程F#:》一书之后尝试的F#中的一些代码

(*
Mega Hello World:
Take two command line parameters and then print
them along with the current time to the console.
*)
open System
[<EntryPoint>]
let main (args : string[]) =
if args.Length <> 2 then
failwith "Error: Expected arguments <greeting> and <thing>"
let greeting, thing = args.[0], args.[1]
let timeOfDay = DateTime.Now.ToString("hh:mm tt")
printfn "%s, %s at %s" greeting thing timeOfDay
// Program exit code
0
main(["asd","fgf"]) |> ignore

main中有一个错误,它说:这个表达式的类型应该是"String[]",但这里的类型是"一个列表"。但是String[]是一个字符串数组。所以我不理解我的错误。

string[]确实是一个字符串数组,但["asd", "fgf"]不是——它是一个列表,这就是为什么会出现错误。

要创建数组,请使用[|"asd"; "fgf"|](请注意,在列表和数组中,;都用作分隔符-,创建元组(。

此外,在标记为EntryPoint的函数之后不能有代码。即使可以,调用该函数也没有意义,因为它已经被命令行参数自动调用了——这就是EntryPoint属性的意义所在。

相关内容