Stream: beginners

Topic: record shorthands


view this post on Zulip itmuckel (Mar 09 2023 at 19:24):

Is this intended to work or are record shorthands only for writing functions that only return a record field?
printCustomer = "Hello \(.name) \(.lastName)!"
My understanding was that this takes a record and prints its fields name and lastName, but that doesn't work.

view this post on Zulip Brendan Hansknecht (Mar 09 2023 at 19:52):

I don't think this is expected to work for a few reasons.

view this post on Zulip Brendan Hansknecht (Mar 09 2023 at 19:53):

you would need to write something like:

printCustomer = \c -> "Hello \(c.name) \(c.lastName)!"

view this post on Zulip Brendan Hansknecht (Mar 09 2023 at 19:56):

The .field syntax is simply a way to implicitly generate an accessor lambda. This gives you something you could pass into another function.

It enables things like:

List.map customers .name

or more generally:

doSomething = \data, getName ->
    name = getName data
    # do some stuff and return something

# calling it
doSomething myCustomer .name

view this post on Zulip Brendan Hansknecht (Mar 09 2023 at 19:57):

so really .name just translates to \x -> x.name

view this post on Zulip itmuckel (Mar 09 2023 at 19:59):

Ah, got it, thank you for the examples :-)


Last updated: Jul 06 2025 at 12:14 UTC