0
votes

I receive a warning for the following code:

    | _ -> let card1::card2::remainingDeck = newDeck
           Some(card1, card2), remainingDeck

Incomplete pattern matches on this expression. For example, the value '[_]' may indicate a case not covered by the pattern(s).

Here's the rest of the code:

let newDeck = [for suit in suits do
                for face in faces do
                    yield {Face=face; Suit=suit}]

let deal = function
    | card1::card2::remaining -> Some(card1, card2), remaining
    | _ -> let card1::card2::remainingDeck = newDeck
           Some(card1, card2), remainingDeck

I have noticed that after several minutes of my editor being idle, when I build my solution again, the warning goes away.

Am I doing something wrong?

1

1 Answers

6
votes

You get the warning because the only information the compiler has about newDeck is its type. It has no information about length of the list - and if newDeck doesn't have at least 2 elements, let card1::card2::remainingDeck = newDeck will throw MatchFailureException.

Actually

let card1::card2::remainingDeck = newDeck
Some(card1, card2), remainingDeck

is equivalent to

match newDeck with
| card1::card2::remainingDeck -> Some(card1, card2), remainingDeck

The compiled MSIL is exactly the same.