dhall-lang / dhall-haskell

Maintainable configuration files
https://dhall-lang.org/
BSD 3-Clause "New" or "Revised" License
908 stars 211 forks source link

Suggesting a new construct for dealing with optional fields in Dhall defaults #2577

Open jslambda opened 3 months ago

jslambda commented 3 months ago

I am working on writing some Elasticsearch queries in Dhall, and defining a value with a lot of parameters can become cumbersome (See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#match-field-params as an example)

Dhall defaults described in https://hackage.haskell.org/package/dhall-1.41.2/docs/Dhall-Tutorial.html#g:12 is a great help, however it requires adding a lot of optional fields in my use-case, which in my opinion, adds noise. I can see that this problem has been discussed already in https://github.com/dhall-lang/dhall-haskell/issues/2434 , and I'd like to follow up with some suggestion for adding a new construct/function to Dhall. I'd like to achieve the benefits of Dhall defaults without having to specify optional fields.

Is it a good idea to add a new construct which desugars into Dhall defaults with optional fields? Let's call the new construct OptValueWithDefault

let ThatValue = OptValueWithDefault({
    Type={age: Natural, name:Text, height:Double}, 
    default={age=25}
})

desugars into

let ThatValue = {
    Type={age : Optional Natural, name : Optional Text, height : Optional Double}, 
    default={age=Some 25, name=None Text, height=None Double}
}

and

ThatValue::{ name = "Jerry" }

will desugar into

ThatValue::{ name = Some "Jerry" }

Is this an acceptable addition to Dhall?

Best regards, Hamid