Forum Discussion

elderfd's avatar
elderfd
Frequent Visitor
8 years ago
Solved

List.Select does not work as expected when List contains Errors

When a List contains Errors, List.Select does not work as expected. First we generate a list with an Error, List = {"Hello", 2, #date("not a date")} The third element of this list is an Error.   ...
  • Ehren's avatar
    8 years ago

    The reason for this behavior is that lists in M are lazy. The previews (in the Query Editor) of both the "each false" and "each true" queries actually produce the same result. In both cases, the Query Editor attempts to show all the values, which causes List.Select to traverse its entire source list. In the process, it encounters the error value, which interrupts its enumeration of the source list, resulting in a list that contains an error.

     

    However, when you navigate into the first item in the resulting list, both the "each false" and "each true" queries attempt to get only the first item resulting from the List.Select. Since the "each true" query's List.Select condition will "match" any input, the “Hello” value is selected and returned, and the error value is never encountered. However, in the "each false" query, the first two inputs do not "match", and thus it moves on to the error value, which interrupts its enumeration and causes the error to bubble up.

     

    Hope this helps clarify things.

    Ehren