F # compiler error "This expression should have a unit of type, but there is a bool type here." expression in expressions {if else}

I wrote such a function in F #:

let TwistBasket (reverse: bool, quarters: int, overTwist: int byref) =
    overTwist <- 50
    WaitForBasketReady()
    waitBasket.Reset()
    let move = 135*quarters - 25 + overTwist
    let speed =
        match reverse with
            | true -> -75y
            | false -> 75y
    let waitHandle = motorBasket.SpeedProfile(speed, 15u, uint32 move, 10u, true)
    Task.Factory.StartNew(fun () ->
        waitHandle.WaitOne()
        if (overTwist <> 0) then
            motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true).WaitOne()
        waitBasket.Set()

In this case, if

    if (overTwist <> 0) then
         motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true).WaitOne()

I get an error: This expression was expected to have type unit but here has type bool.

Actually motorBasket.SpeedProfile().WaitOne()returns a logical instruction. I need it.

Because I'm trying to convert this if else statement to C #:

        Task.Factory.StartNew(() =>
        {
            waitHandle.WaitOne();
            if (overTwist != 0)
            {
                motorBasket.SpeedProfile((sbyte) -speed, 0, (uint) overTwist, 0, true).WaitOne();
            }
            waitBasket.Set();
        });

How can I fix my mistake?

+4
source share
2 answers

Studying the C # version, it does nothing with the result. Therefore, in F # I would say ignorewhich will have the result:

if (overTwist <> 0) then
     motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true).WaitOne() |> ignore

F # is more strict than C # in these cases, if your if .. then it has no branch, then the result should be one, which makes sense.

else () () , , , ? , , , F # .

+3

F # if/else . if else, F # , else unit, , . , else:

if (overTwist <> 0) then
    motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true).WaitOne()
else
    ... // Put here expression of the same type as if (I assume it bool)

ignore , .

+2

Source: https://habr.com/ru/post/1569650/


All Articles