Which cases are not covered in this example?

I get a warning when I compile the following. But I think my switch statement covers all the cases, so what am I doing wrong?

Warning 8: You forgot to handle a possible case here, for example:
[_]

let an_array = n => {
  if n < 5 {
    []
  } else {
    [2, 3]
  }
}

let a = switch an_array(4) {
| [_, _] => "non-empty"
| [] => "empty"
}

Unfortunately inifinite cases like [_, _, _] and [_, _, _, _, _, _, _, _], …
The switch case is not recommended for arrays (mentioned in the docs). If it is an array of a fix length, use a tuple or in this simple case you can check the length of the array instead.

1 Like

Suggestion of @dkirchhof is absolutely correct. However you can do what you are trying above with a list. (PG Link)

let a_list = (n:int) =>
  if n < 5 {
    list{}
  } else {
    list{2, 3}
  }

let a = switch (5 -> a_list) {
| list{} => "empty"
| list{_, ..._} => "non-empty"
}

The reason above code is not throwing a warning is because lists allow you do to hd::tail kind of “cons” thing thus covering all possible cases.

(I am a newbie so take my advice with a grain of salt. and follow the advice given by @dkirchhof lol )

1 Like
let a = switch an_array(4) {
| [] => "empty"
| _ => "non-empty"
}
2 Likes

That was a simplified example, I actually need the value in the X position in the array.