Stream: beginners

Topic: Help constructing a literal record


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

I ned some help constructing a literal record in Roc.

I'm trying to get up the first test to pass for my new function getNextRecord.

I just want to start with the base case of returning a record/object that indicates there are no items in the next row.

But I'm a bit stumped figuring out how to create a literal record in Roc.

My first guess was that it was going to be something like this:

getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }
getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
    {
        row = {
            items = [],
            galleryWidth = galleryWidth,
            targetRowHeight = targetRowHeight,
            currentRowItems = currentRowItems,
            width = width,
            removedItems = removedItems,
            headings = headings
        },
        removedItems = [],
        remainingItems = items
    }

That didn't work.

I searched through the tutorial and found "record builder syntax" which got me to this:

getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }
getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
    {
        row: <- {
            items: <- [],
            galleryWidth: <- galleryWidth,
            targetRowHeight: <- targetRowHeight,
            currentRowItems: <- currentRowItems,
            width: <- width,
            removedItems: <- removedItems,
            headings: <- headings
        },
        removedItems: <- [],
        remainingItems: <- items
    }

expect getNextRow [] 10  20 [] 0 [] [] == {
    row: <- {
        items: <- [],
        galleryWidth: <- 10,
        targetRowHeight: <- 20,
        currentRowItems: <- [],
        width: <- 0,
        removedItems: <- [],
        headings: <- []
    },
    removedItems: <- [],
    remainingItems: <- = []
}

I'm not sure what I have wrong here.

Error:

── UNAPPLIED RECORD BUILDER in Layout.roc ──────────────────────────────────────

This record builder was not applied to a function:

23│>      {
24│>          row: <- {
25│>              items: <- [],
26│>              galleryWidth: <- galleryWidth,
27│>              targetRowHeight: <- targetRowHeight,
28│>              currentRowItems: <- currentRowItems,
29│>              width: <- width,
30│>              removedItems: <- removedItems,
31│>              headings: <- headings
32│>          },
33│>          removedItems: <- [],
34│>          remainingItems: <- items
35│>      }

However, we need a function to construct the record.

Note: Functions must be applied directly. The pipe operator (|>) cannot be used.

I was hoping to use an anonymous type, but then I thought maybe what I'm doing wrong is that I'm not naming the type.

So I tried naming it:

GetNextRowResult : { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }

getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> GetNextRowResult
getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
    GetNextRowResult {
        row: <- {
            items: <- [],
            galleryWidth: <- galleryWidth,
            targetRowHeight: <- targetRowHeight,
            currentRowItems: <- currentRowItems,
            width: <- width,
            removedItems: <- removedItems,
            headings: <- headings
        },
        removedItems: <- [],
        remainingItems: <- items
    }

expect getNextRow [] 10  20 [] 0 [] [] == GetNextRowResult {
    row: <- {
        items: <- [],
        galleryWidth: <- 10,
        targetRowHeight: <- 20,
        currentRowItems: <- [],
        width: <- 0,
        removedItems: <- [],
        headings: <- []
    },
    removedItems: <- [],
    remainingItems: <- = []
}

That gets me this error:

── RECORD PARSE PROBLEM in Layout.roc ──────────────────────────────────────────

I am partway through parsing a record, but I got stuck here:

 1│  interface Layout
 2│      exposes [getNextRow, GalleryItem, LayoutItem, LayoutRow]
 3│      imports []
 4│
 5│  GalleryItem : {
 6│  }
 7│
 8│  LayoutItem : {
 9│  }
10│
11│  LayoutRow : {
12│      items : List LayoutItem,
13│      galleryWidth : Int U32,
14│      targetRowHeight : Int U32,
15│      currentRowItems : List GalleryItem,
16│      width : Int U32,
17│      removedItems : List GalleryItem,
18│      headings : List Str
19│  }
20│
21│  GetNextRowResult : { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }
22│
23│  getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> GetNextRowResult
24│  getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
25│      GetNextRowResult {
26│          row: <- {
27│              items: <- [],
28│              galleryWidth: <- galleryWidth,
29│              targetRowHeight: <- targetRowHeight,
30│              currentRowItems: <- currentRowItems,
31│              width: <- width,
32│              removedItems: <- removedItems,
33│              headings: <- headings
34│          },
35│          removedItems: <- [],
36│          remainingItems: <- items
37│      }
38│
39│  expect getNextRow [] 10  20 [] 0 [] [] == GetNextRowResult {
                                                                ^

TODO provide more context

I'm not sure what to do now.

Can anyone point me to an example of creating a literal record?

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

They have a : based syntax.

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

Also, that is a really good point that record builder syntax may be a terrible name.

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

That is an advanced feature.

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

Directly porting your example should be this:

getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }
getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
    {
        row: {
            items: [],
            galleryWidth: galleryWidth,
            targetRowHeight: targetRowHeight,
            currentRowItems: currentRowItems,
            width: width,
            removedItems: removedItems,
            headings: headings
        },
        removedItems: [],
        remainingItems: items
    }

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

Note, you don't need the name: part if the name matches a variable name. So can be shorten to:

getNextRow : List GalleryItem, Int U32, Int U32, List GalleryItem, Int U32, List GalleryItem, List Str -> { row : LayoutRow, removedItems: List GalleryItem, remainingItems : List GalleryItem }
getNextRow = \items, galleryWidth, targetRowHeight, currentRowItems, width, removedItems, headings ->
    {
        row: {
            items: [],
            galleryWidth,
            targetRowHeight,
            currentRowItems,
            width,
            removedItems,
            headings
        },
        removedItems: [],
        remainingItems: items
    }

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

tutorial section on records: https://www.roc-lang.org/tutorial#records

view this post on Zulip Ashley Davis (Jul 04 2024 at 21:44):

That's awesome thank you so much.


Last updated: Sep 24 2026 at 15:59 UTC