eww icon indicating copy to clipboard operation
eww copied to clipboard

[FEATURE] Safe Access Syntax

Open oldwomanjosiah opened this issue 2 years ago • 2 comments

Description of the requested feature

When writing my configuration files, I often find myself writing something like

(some-widget
  :some-arg { global-state["optional-value"] != "" ? "prefix${global-state["optional-value"]}" : "" })

Seeing as we already have the Elvis operator :? (with the "" value being considered equivalent to None), this proposes adding a new syntax for doing the opposite.

Proposed configuration syntax

The syntax for this, heavily inspired by Kotlin's safe access operator.

The two main syntaxual forms are as follows

{ global-state["optional-value"]?.let { gval -> "prefix${gval}" }

{ global-state["optional-value"]?.let { "prefix${it}" }

Where in the second, we elide the binding identifier, opting to use the implicit it. In kotlin, it is standard practice to outlaw name shadowing (including the implicit it), but I don't know that that would be strictly necessary.

In kotlin this functionality is actually the confluence of 3 separate things:

  • Safe Call/Access
  • Extension functions (let is a generic inline extension on all types)
  • Trailing Lambda Syntax (no parens around { gval -> ... })

I am not suggesting implementing all of the above, as that would be too heavy for simplexpr, but instead just this specific usage as it would round out the elvis operator and greatly simplify writing configurations.

Additional context

No response

oldwomanjosiah avatar Sep 12 '22 03:09 oldwomanjosiah

If accepted, I would be happy to work on the implementation as I have been meaning to begin contributing here anyways.

oldwomanjosiah avatar Sep 12 '22 03:09 oldwomanjosiah

Adding the full kotlin .let syntax is not something I'd want to do currently -- partly because I think it'd be rather confusing for most users, and partly because if eww ever adds some form of lambda (for local state related stuff, see the discussion about local state syntax) then that syntax should be very carefully considered to work with stuff like this, too.

However, adding optional access (foo?.bar) like kotlin has it, I'd be perfectly happy to have!

elkowar avatar Sep 13 '22 09:09 elkowar