Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

f# - Block is unfinished

This code fragment is compiled:

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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 ()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...