相关疑难解决方法(0)

F#中的printfn类型,静态与动态字符串

我刚刚开始在Mono中使用F#,并且出现了以下问题,我无法理解.查找资料printfn,并TextWriterFormat没有带来任何启示,所以我想我要问在这里.

在FSI中,我执行以下操作:

> "hello";;
val it : string = "hello"
> printfn "hello";;
hello
val it : unit = ()
Run Code Online (Sandbox Code Playgroud)

只是一个普通的字符串并打印它.精细.现在我想声明一个变量来包含相同的字符串并打印它:

> let v = "hello" in printfn v ;;
let v = "hello" in printfn v ;;
---------------------------^
\...\stdin(22,28): error FS0001: The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>'
Run Code Online (Sandbox Code Playgroud)

我从阅读中理解,printfn需要一个恒定的字符串.我也明白,我可以解决这个问题printfn "%s" v.

但是,我想了解这里输入的内容.显然,"hello"类型string也是如此v.为什么会出现类型问题?有printfn什么特别的吗?据我所知,编译器已经对第一个字符串的参数执行了类型检查,这样就printfn "%s" 1失败了......这当然不适用于动态字符串,但我认为这只是编译器方面的一个方便.静态案例.

f#

25
推荐指数
3
解决办法
4421
查看次数

标签 统计

f# ×1