Zooce / zomb-file

The ZOMB File Format Specification
MIT License
1 stars 0 forks source link

[Feature Request] Default macro parameter values #1

Closed Zooce closed 3 years ago

Zooce commented 3 years ago

Allow default macro parameter values like this:

$item(id, label = null) = {
    id = %id
    label = %label
}

item_1 = $item(Hello)
item_2 = $item(Hello, "What's up?")

Default parameter values are common in programming languages (for better or worse -- there are debates on this), and so I think in general it's pretty easy to see why this features might be useful. Now obviously the ZOMB file format is not a programming language, but default macro parameters would allow for less duplication, which is kind of the most important objective for ZOMB files.

As for implementations I don't think this feature would require much extra effort -- maybe an extra parameter count check and some logic to replace the default value of a parameter if a value was provided in the macro expression -- of course it depends on the implementation.

An alternative

This is already kind of achievable without default parameters, and in some ways I actually like this better because it's more explicit:

$item_w_label(id, label) = {
    id = %id
    label = %label
}
$item(id) = $item_w_label(%id, null)

item_1 = $item(Hello)
item_2 = $item_w_label(Hello, "What's up?")
Zooce commented 3 years ago

The more I think about this, the more I'm convinced that it aligns with the 1st goal of ZOMB files -- "No more unwieldy repetition."

The alternative in my original post is unaffected in this feature were to be added to ZOMB files, so you get both.

Zooce commented 3 years ago

Completed by #5