This is a construct that I think @gchiu originally suggested, which is kind of a reverse-DEFAULT. A variable is optionally updated, but left as-is if the value you try to update to is void.
>> x: <a>
== <a>
>> x: perhaps if false [<b>]
== <a>
>> x
== <a>
>> x: perhaps if true [<c>]
== <c>
>> x
== <c>
An implementation of this would look something like:
perhaps: enfixed func* [
"Set word or path to a default value if that value is a value"
return: [<opt> any-value!]
'target [set-word! set-path!]
"The word to which might be set"
optional [<opt> <void> any-value!]
"Value to assign only if it is not void"
][
if semiquoted? 'optional [
;
; DEFAULT requires a BLOCK!, PERHAPS does not. Catch `x: perhaps [...]`
;
fail 'optional [
"Literal" type of :optional "used w/PERHAPS, use () if intentional"
]
]
; Note that right evaluates before left here:
; https://github.com/rebol/rebol-issues/issues/2275
;
if null? :optional [return get/any target]
return set target :optional
]
It's a nice thing to have, but I'm not sure what to call it. Once upon a time it was called MAYBE, before the term was taken for the purpose of converting nulls to voids (which I think is a good fit).
PERHAPS seems too indistinguishable from MAYBE, and I don't know there's any clear reasoning as to why it would be called that.
I think UPDATE was suggested:
>> x: 10
== 10
>> x: update void
== 10
>> x
== 10
>> x: update 20
== 20
>> x
== 20
But I dunno about that either. Ideas?