【问题标题】:args[] in Main for F#F# 的 Main 中的 args[]
【发布时间】:2020-03-03 05:10:21
【问题描述】:

这是我在 Chris Smith 的 Programming 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[]'但是这里是类型“a list”。但是 string[] 是一个字符串数组。所以我不明白我的错误。

【问题讨论】:

    标签: f#


    【解决方案1】:

    string[] 确实是一个字符串数组,但 ["asd", "fgf"] 不是 - 它是一个列表,这就是您收到该错误的原因。

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

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

    【讨论】:

    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2019-09-26
    相关资源
    最近更新 更多