# Daml support for named arguments

**URL:** <https://forum.canton.network/t/daml-support-for-named-arguments/2393>\
**Category:** App Development\
**Created:** [April 7, 2021, 2:22pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393 "2021-04-07T14:22:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amiracam](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@amiracam](https://forum.canton.network/u/amiracam)\
**Post date:** [April 7, 2021, 2:22pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/1 "2021-04-07T14:22:08Z")

</div>

daml would be quite beautiful or should I say even more beautiful if it were to support named arguments 🙂 , if its not in the works already pls accept it as a feature request

---

<div class="post-metadata">

**Author:** ![Sofia\_Faro](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sofia_faro/32/168_2.png) [@Sofia\_Faro](https://forum.canton.network/u/Sofia_Faro)\
**Post date:** [April 7, 2021, 3:24pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/2 "2021-04-07T15:24:17Z")

</div>

Daml is based on Haskell, which doesn’t directly support named arguments. However, there’s a few ways of achieving the same result by taking advantage of the type system.

Let me show you how to do this by working through an example.

Here’s a normal function that takes a first name, a last name, an age, and then returns a text:

```haskell
makeDescription : Text -> Text -> Int -> Text
makeDescription firstName lastName age =
  implode [firstName, " ", lastName, " is ", show age, " years old."] 

```

For example, running:

```haskell
makeDescription "John" "Doe" 30

```

returns:

```haskell
"John Doe is 30 years old."

```

Now how do we give this function named arguments in Daml?

### Method #1: Use a Record Type

The first approach is to bundle all the arguments together into a record type. Then, when calling the function, you just have to pass in the record type. Because of the built-in support for named record fields, this is like giving the function a set of named arguments.

For example, we would define a record type:

```haskell
data Person = Person with
    firstName : Text
    lastName : Text
    age : Int

```

And then use the record in the function:

```haskell
makeDescription : Person -> Text
makeDescription p = 
  implode [p.firstName, " ", p.lastName, " is ", show p.age, " years old."]

```

The function call would look like this (using `with` syntax):

```haskell
makeDescription Person with
  firstName = "John"
  lastName = "Doe"
  age = 30

```

Or like this (using curly braces):

```haskell
makeDescription Person { firstName = "John", lastName = "Doe", age = 30 }

```

This approach bundles all the arguments together in a single type, which means you can do other useful things with them, e.g. you could define a `johnDoe` value and pass it in at once:

```haskell
johnDoe : Person
johnDoe = Person { firstName = "John", lastName = "Doe", age = 30 }

makeDescription johnDoe

```

It also makes it easy to specify arguments that only differ in one or two fields, for example:

```haskell
makeDescrption johnDoe { firstName = "Jane" }

```

returns

```haskell
"Jane Doe is 30 years old."

```

As you can see, this is a very flexible approach, and it supports both “named arguments” (by using record field names) and “default/optional arguments” (by supplying a default record value and changing only the fields you need).

### Method #2: Use Wrapper Types

Another approach that comes up is to wrap each argument up in a separate wrapper type. This way, the wrapper type acts like a “name” for the argument.

For example, we would define a wrapper type for each argument:

```haskell
data FirstName = FirstName Text
data LastName = LastName Text
data Age = Age Int

```

Then we use these to define the function:

```haskell
makeDescription : FirstName -> LastName -> Age -> Text
makeDescription (FirstName firstName) (LastName lastName) (Age age) =
  implode [firstName, " ", lastName, " is ", show age, " years old."] 

```

Then the call would be:

```haskell
makeDescription (FirstName "John") (LastName "Doe") (Age 30)

```

This approach treats each argument separately, so if you have other related functions you may find it quite useful to reuse wrappers. It also makes the function type signature much more descriptive, which is a nice bonus. Also, if you provide the arguments in the wrong order by accident, the compiler will detect the error.

In my day-to-day programming in Daml and Haskell, I see and use both of these approaches. Personally I prefer the second approach, because I think it’s easier to reuse the wrapper types and because they provide a little bit more type safety (you can’t accidentally skip an argument), but this approach doesn’t really support optional arguments. So each method has its upsides and downsides.

---

<div class="post-metadata">

**Author:** ![Leonid\_Rozenberg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_rozenberg/32/48_2.png) [@Leonid\_Rozenberg](https://forum.canton.network/u/Leonid_Rozenberg)\
**Post date:** [April 7, 2021, 4:15pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/3 "2021-04-07T16:15:54Z")

</div>

How do these two methods impact the performance of Daml code?

---

<div class="post-metadata">

**Author:** ![amiracam](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@amiracam](https://forum.canton.network/u/amiracam)\
**Post date:** [April 7, 2021, 7:00pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/4 "2021-04-07T19:00:30Z")

</div>

thanks , the first approach is quite similar to the passing of a Map approach Groovy used before their more explicit support for named arguments i.e. via the @NamedVariant and kin annotation.

Nice to know

---

<div class="post-metadata">

**Author:** ![Sofia\_Faro](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/sofia_faro/32/168_2.png) [@Sofia\_Faro](https://forum.canton.network/u/Sofia_Faro)\
**Post date:** [April 8, 2021, 9:10am UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/5 "2021-04-08T09:10:57Z")

</div>

> How do these two methods impact the performance of Daml code?

Both approaches are going to be slower than passing the argument directly. I don’t have numbers, but this is what I understand based on the runtime representation.

In the first approach, you’re paying the cost to construct the record, and the cost to access the record fields.

In the second approach, you’re paying the cost to wrap each argument and the cost to unwrap each argument. There is some cost mitigation if you can reuse the same wrapper types and only unwrap them when you have to (e.g. pass around `FirstName` instead of `Text`, unwrap only when you need to).

If you don’t have a lot of opportunities to reuse the wrapper types, or you have a lot of arguments, the first approach will probably be more efficient than the second approach.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [April 8, 2021, 9:44am UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/6 "2021-04-08T09:44:39Z")

</div>

> [@Sofia\_Faro](#):
>
> And then use the record in the function:
> 
> ```haskell
> makeDescription : Person -> Text
> makeDescription p = 
> implode [p.firstName, " ", p.lastName, " is ", show p.age, " years old."]
> 
> ```

I’d add that you can also use record puns when de-structuring, so you could write:

```haskell
makeDescription : Person -> Text
makeDescription Person{firstName, lastName, age} = 
  implode [firstName, " ", lastName, " is ", show age, " years old."]

```

And even do variations of this like

```haskell
makeDescription Person{firstName=name, lastName=last, ..} = 

```

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [April 8, 2021, 1:17pm UTC](https://forum.canton.network/t/daml-support-for-named-arguments/2393/7 "2021-04-08T13:17:13Z")

</div>

I don’t much like it personally, but for the sake of completeness, you can also pun all the way:

```haskell
makeDescription : Person -> Text
makeDescription p{..} = 
  implode [firstName, " ", lastName, " is ", show age, " years old."]

```

where the destructuring form `p{..}` brings all the fields of `p` into the local scope directly as local variables.
