The correct binding style in F #

One of the most subtle uses of the do keyword in F # is the ability to comment on expressions that return a unit (aka statements). For example, I often do this in function definitions so that it is clear that the return type is one, and also instruct the compiler type to check this out for me. For instance:

let restart agent = do agent.Post Restart

However, I'm not sure that the dominant position of the do keyword is in the F # community.

How can I use the 'do' keyword in this example:

// Annotate each statement
let move source destination =
    do copy source destination
    do clear source

// Annotate the statements as one block
let move source destination =
    do
        copy source destination
        clear source

// Just annotate the "return" statement
let move source destination =
    copy source destination
    do clear source

// Perhaps don't annotate at all?
let move source destination =
    copy source destination
    clear source
+4
source share
1 answer

do, F # ( unit). , , FS0020 .

let move source destination =
    copy source destination
    clear source

:

  • copy clear unit

    .

  • copy ,

    FS0020 :

    copy source destination int, . , , . ignore (copy source destination) , . let result = copy source destination

  • copy unit, clear , unit

    , move , clear, move, , .

, do : , , move ( - move - - move ..). ​​ - clear (, , , ), !

, , (IMO) , , . , F # , (#light "off") end, , do .

+3

All Articles