Stream: beginners

Topic: My next newbie issue: Type mismatch


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

I'm working towards adding the next test for my getNextRow function.

The code I need to add next means adding some fields to my record type. Previously it was just an empty record type:

GalleryItem : {
}

Now I've added the fields I need:

GalleryItem : {
    width : Int U32,
    height : Int U32,
    headings : List Str,
}

I created a helper function that I can use in my tests to produce a default gallery item (not sure if my syntax is quite right here):

# Makes a gallery item for testing.
makeDefaultItem : _ -> GalleryItem
makeDefaultItem = \_ -> {
    width: 10,
    height: 20,
    headings: []
}

Now I use that function to create some lists to pass into my function for testing:

# No items left returns current row.
expect
    currentRowItems = [makeDefaultItem, makeDefaultItem, makeDefaultItem]
    removedItems = [makeDefaultItem, makeDefaultItem]
    headings = ["a", "b"]
    out = getNextRow [] 10 21 currentRowItems 12 removedItems headings
    out == {
        row: {
            items: [{}, {}],
            offsetY: 0,
            width: 12,
            height: 21,
            headings,
        },
        removedItems,
        remainingItems: []
    }

My updated code results in multiple type mismatches, but I'm stuck on the first one:

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

This 4th argument to getNextRow has an unexpected type:

73│      out = getNextRow [] 10 21 currentRowItems 12 removedItems headings
                                   ^^^^^^^^^^^^^^^

This currentRowItems value is a:

    List (* -> GalleryItem)

But getNextRow needs its 4th argument to be:

    List {
        headings : List Str,
        height : Int U32,
        width : Int U32,
    }

My feeling is that I have supplied what it needs, but the error tells me that the types are somehow wrong.

This is the type signature of getNextRow:

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 ->

Anyone know what's wrong here?

I've pushed full code with all the errors here: https://github.com/ashleydavis/book-of-examples/blob/main/gallery/roc/layout.roc

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

Int U32 should just be U32

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

Cause U32 is something like: U32 : Int Unsigned32

view this post on Zulip Richard Feldman (Jul 11 2024 at 22:30):

I think we should consider special-casing that one

view this post on Zulip Richard Feldman (Jul 11 2024 at 22:30):

things like Int I32

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

I don't think that is it.

I'd already been using Int U32 in multiple places without a problem yet.

But I replaced "Int U32" with "U32" across the file and I still get the same error.

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

This 4th argument to getNextRow has an unexpected type:

73│      out = getNextRow [] 10 21 currentRowItems 12 removedItems headings
                                   ^^^^^^^^^^^^^^^

This currentRowItems value is a:

    List (* -> GalleryItem)

But getNextRow needs its 4th argument to be:

    List {
        headings : List Str,
        height : U32,
        width : U32,
    }

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

Oh, wow, surprised that works. Let me look deeper

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

I think you need to be calling makeDefaultItem in lines like [makeDefaultItem, makeDefaultItem, makeDefaultItem]

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

[makeDefaultItem {}, makeDefaultItem {}, makeDefaultItem {}]

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

That or you need getNextRow to take a list of functions that generate GalleryItem

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

Though in this case, I would probably just change makeDefaultItem to defaultItem and make it not a function at all

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

defaultItem : GalleryItem
defaultItem = {
    width: 10,
    height: 20,
    headings: []
}

view this post on Zulip Richard Feldman (Jul 11 2024 at 23:57):

Ashley Davis said:

I'd already been using Int U32 in multiple places without a problem yet.

that's very surprising! It shouldn't work :sweat_smile:

view this post on Zulip Richard Feldman (Jul 11 2024 at 23:58):

do you happen to have a code example which uses Int 32 and works? I'd like to figure out what's going on with that haha

view this post on Zulip Ashley Davis (Jul 15 2024 at 21:41):

Well I wasn 't doing any calculations with "Int U32" yet but it did compile and I was able to pass them into and out of a function.

view this post on Zulip Ashley Davis (Jul 15 2024 at 21:46):

Brendan Hansknecht said:

Oh, wow, surprised that works. Let me look deeper

Hi @Brendan Hansknecht I tried making the change you suggested. What does it mean to add empty curly brackets onto a function call?

It does compile now which is good, but trying to figure out why latest test started failing:

── EXPECT FAILED in Layout.roc ─────────────────────────────────────────────────

This expectation failed:

68│>  # No items left returns current row.
69│>  expect
70│>      currentRowItems = [makeDefaultItem {}, makeDefaultItem {}, makeDefaultItem {}]
71│>      removedItems = [makeDefaultItem {}, makeDefaultItem {}]
72│>      headings = ["a", "b"]
73│>      out = getNextRow [] 10 21 currentRowItems 12 removedItems headings
74│>      out == {
75│>          row: {
76│>              items: [{}, {}],
77│>              offsetY: 0,
78│>              width: 12,
79│>              height: 21,
80│>              headings,
81│>          },
82│>          removedItems,
83│>          remainingItems: []
84│>      }

When it failed, these variables had these values:

currentRowItems : List GalleryItem
currentRowItems = [{ headings: [], height: 20, width: 10 }, { headings: [], height: 20, width: 10 }, { headings: [], height: 20, width: 10 }]

removedItems : List GalleryItem
removedItems = [{ headings: [], height: 20, width: 10 }, { headings: [], height: 20, width: 10 }]

headings : List Str
headings = ["a", "b"]

out : {
    remainingItems : List GalleryItem,
    removedItems : List GalleryItem,
    row : {
        headings : List Str,
        height : Int Unsigned32,
        items : List {},
        offsetY : Int Unsigned32,
        width : Int Unsigned32,
    },
}
out = { remainingItems: [], removedItems: [{ headings: [], height: 20, width: 10 }, { headings: [], height: 20, width: 10 }], row: { headings: ["a", "b"], height: 21, items: [{}, {}, {}], offsetY: 0, width: 12 } }

view this post on Zulip Ashley Davis (Jul 15 2024 at 21:48):

Also @Brendan Hansknecht I actually need makeDefaultItem to be a function because soon I want to add optional parameters to it configure it differently for different tests. It needs be a kind of constructor that gives you standard defaults when you don't supply them.

view this post on Zulip Ashley Davis (Jul 15 2024 at 21:52):

@Richard Feldman the message for the failing test doesn't seem to compare actual results vs expected. I think it just prints out the actual results.

view this post on Zulip Ashley Davis (Jul 15 2024 at 21:54):

I got it working (yay), but would have been easier if the failing test message shows the diff between actual and expected.

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

There is no such thing as a zero arg function in roc.

In fact, you can see the one arg for makeDefault, \_. In Roc, _ is a placeholder for ignored values.

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

You technically could pass anything into makeDefault since the value is ignored, but the standard ignored arg in roc is an empty record {}

view this post on Zulip Richard Feldman (Jul 15 2024 at 22:05):

Ashley Davis said:

I got it working (yay), but would have been easier if the failing test message shows the diff between actual and expected.

yeah I'd like to do this! It's a project that nobody is working on at the moment, but if anyone is interested, I can give pointers on where to get started :big_smile:

view this post on Zulip Luke Boswell (Jul 15 2024 at 22:08):

Before we forget, we should make an issue an link to that zulip chat. Didn't we talk about the diff somewhere recently?

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

Luke Boswell said:

Before we forget, we should make an issue an link to that zulip chat. Didn't we talk about the diff somewhere recently?

I think this one: https://roc.zulipchat.com/#narrow/stream/231634-beginners/topic/Type.20mismatch.3A.20Do.20I.20need.20explicit.20typing.20here.3F

view this post on Zulip Luke Boswell (Jul 15 2024 at 22:58):

I thought there was one where Richard posted with a diff view he proposed

view this post on Zulip Luke Boswell (Jul 15 2024 at 22:59):

Yeah this one https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/diffs.20in.20error.20messages/near/450008154


Last updated: Jul 06 2025 at 12:14 UTC