Stream: beginners

Topic: Record inference in string interpolation


view this post on Zulip Shawn (Jan 30 2024 at 07:06):

How come this doesn't work? It doesn't seem to infer that .firstName and .lastName need to be strings

main =
    Stdout.line (greet { firstName: "Rocy", lastName: "Balboa" })

greet = "Hello $(.firstName) $(.lastName)!"

view this post on Zulip Shawn (Jan 30 2024 at 07:07):

── TYPE MISMATCH in main.roc ───────────────────────────────────────────────────

This argument to this string interpolation has an unexpected type:

15│  greet = "Hello $(.firstName) $(.lastName)!"
                                    ^^^^^^^^^

This .lastName value is a:

    { lastName : a }b -> a

But this string interpolation needs its argument to be:

    Str

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

view this post on Zulip Kiryl Dziamura (Jan 30 2024 at 07:36):

String interpolation is a syntax sugar for Str.concat so roc expects all passed arguments to be strings (see Str.concat type)

But .firstName and .lastName are getters of a record field

Also, roc has no currying so the function must be defined explicitly. So greet in your example is not a function but a string. As a result, you want smth like:

main =
    Stdout.line (greet { firstName: "Rocy", lastName: "Balboa" })

greet = \user -> "Hello $(user.firstName) $(user.lastName)!"

Last updated: Jul 06 2025 at 12:14 UTC