块未完成 [英] Block is unfinished

查看:96
本文介绍了块未完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码段已编译:


let test =
    let x = 1
    printfn "%A" x

如果删除了最后一行,则会出现以下编译错误:

If the last line is removed, there is the following compilation error:


error FS0588: Block following this 'let' is unfinished. Expect an expression.

此消息是什么意思?在C#/C ++中,我期望在这种情况下出现未使用的变量"警告,但是F#会提供一些不同的东西.

What does this message mean? In C#/C++ I would expect the "Unused variable" warning in such situation, but F# gives something different.

推荐答案

在F#中,函数必须将名称绑定到值.

In F#, a function has to bind a name to a value.

printfn语句具有返回值,这最终是绑定到test的内容.

The printfn statement has a return value, and this is ultimately what gets bound to test.

没有printfn语句,您只有一条将值1绑定到x的语句.编译器将期望进行测试.因为测试功能在这一点上停止了,所以这就是为什么您看到编译器错误的原因.

Without the printfn statement you only have a statement binding the value 1 to x. The compiler will be expecting something to bound to test. Because the test function stops at this point, this is why you are seeing the compiler errror.

如果您只希望函数执行某些操作(可能带有副作用),则应以()

If you want your function just to do stuff (possibly with side effects), you should end your function with ()

这篇关于块未完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆