ion-docs icon indicating copy to clipboard operation
ion-docs copied to clipboard

Consider replacing or augmenting `if_void` with `if_some`

Open popematt opened this issue 1 year ago • 1 comments

Currently the signature for if_void is (expression_to_test output_if_void output_if_not_void).

Combined with the semantics for a void field, it leads to patterns like this:

(macro lst_append (string::symbols*)
    (if_void symbols 
             (void)                  // Produce nothing if no symbols provided.
             $ion_symbol_table::{
                 imports: (literal $ion_symbol_table),
                 symbols: [symbols]
             }
    )
)

However, if we had if_some with signature (expression_to_test output_if_some output_if_not_some), then output_if_not_some could be essentially like an optional, trailing parameter, allowing if_some to be concisely used as a "map if not void" operation—like maybeNull?.let { /* ... */ } in Kotlin. For example:

(macro lst_append (string::symbols*)
    (if_some symbols 
         $ion_symbol_table::{
             imports: (literal $ion_symbol_table),
             symbols: [symbols]
         }
    )
)

I suspect that the use cases where you would elide the last argument to if_some are much greater than the use cases where you would want to elide the last argument to if_void.

popematt avatar Aug 29 '24 05:08 popematt

I think if_some is a good idea, your use case is convincing.

I'm not sure that it's a good replacement for if_void in its original use case of "filling in" an absent optional parameter. But that might be better served by a dedicated form that selects the first non-empty stream.

toddjonker avatar Sep 03 '24 21:09 toddjonker

Completed in #342

popematt avatar Dec 04 '24 21:12 popematt