Stream: beginners

Topic: Getting an error message that I don't understand


view this post on Zulip Ashley Davis (Jul 17 2024 at 22:11):

I'm extracting a the first record from a list and then trying to use use a field from it:

        item = List.first items
        aspectRatio = item.width / item.height

And getting this error:

── TYPE MISMATCH in Layout.roc ─────────────────────────────────────────────────

This expression is used in an unexpected way:

37│          aspectRatio = (Num.toFrac item.width) / (Num.toFrac item.height)
                                       ^^^^^^^^^^

This item value is a:

    Result GalleryItem [ListWasEmpty]

But you are trying to use it as:

    { width : * }b

────────────────────────────────────────────────────────────────────────────────

1 error and 3 warnings found in 849 ms

Does anyone know what this means?

Full code: https://github.com/ashleydavis/book-of-examples/blob/main/gallery/roc/layout.roc

view this post on Zulip Ashley Davis (Jul 17 2024 at 22:12):

Also note that I'm checking prior if the list is empty:

    if List.len items == 0 then {
        row: {
            items: List.map currentRowItems \galleryItem -> {}, # Produces layout items from gallery items.
            offsetY: 0,
            height: targetRowHeight,
            width,
            headings
        },
        removedItems,
        remainingItems: []
    }
    else
        item = List.first items
        aspectRatio = item.width / item.height
        ...
  }

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:15):

List.first will always return a result.

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:15):

So you should just use when List.first ... is directly

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:16):

That will check if the list is empty and return an error if so.

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:16):

Then you can move your empty list logic there.

view this post on Zulip Ashley Davis (Jul 17 2024 at 22:17):

So I can't just capture the first item in the list then use it?

view this post on Zulip Ashley Davis (Jul 17 2024 at 22:17):

Do I have to unpack it from the "result" somehow?

view this post on Zulip Ashley Davis (Jul 17 2024 at 22:18):

I haven't used Result yet.

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:18):

Roc will always do a bounds check and return a result.

You could match on the result and crash in the error case if you know it is impossible, but it is definitely advised to use List.first as your "is empty" check instead.

view this post on Zulip Brendan Hansknecht (Jul 17 2024 at 22:22):

So the suggested way to write your code above would be something like:

when List.first items is
    Ok item ->
        aspectRatio = item.width / item.height
        ...
    Err ListWasEmpty ->
        {
            row: {
                items: List.map currentRowItems \galleryItem -> {}, # Produces layout items from gallery items.
                offsetY: 0,
                height: targetRowHeight,
                width,
                headings
            },
            removedItems,
            remainingItems: []
        }

view this post on Zulip Ashley Davis (Jul 17 2024 at 22:26):

Thanks so much.


Last updated: Jul 06 2025 at 12:14 UTC