SET-GROUP! Finds its Groove

Having seen the slick multi-return behavior for SET-BLOCK!, I wondered if there was a better purpose for SET-GROUP! than merely a synonym for SET.

(word): value
set word value

That felt a bit anticlimactic. You save one character, but you add three non-alphabetic symbols.

If these were equivalent, were we really getting the bang for the buck we would want out of a sparkly new datatype?

So I hesitated to declare that to be the meaning.

But I've Found The Reason Why It Needs To Be The Meaning...

It fills the role of being a SINGLE value, that can sit in literal slots to represent an assignment.

That means you can say things like:

(var): default [10]

It's impossible for this to work with:

set var default [10]

Because it splits the intent to set the word pointed to by the variable into a function call to SET and a WORD! reference to VAR. DEFAULT can only quote value left, and it has to pack up all the information it needs to do its job.

So it really is a missing piece. And really, I think there are a lot of cases where I'm just going to like (word): it better than set word. It may be a bit more symbolic, but by not being wordy it could blend in better.

var1: blah blah blah
var2: blah blah blah
(word): blah blah blah  ; doesn't break the rhythm as much as SET WORD

I'll Start Patching In Behaviors For It... The First Is UPARSE EMIT!

Right now, if you know the name you're going to emit for a variable, you can use it literally in GATHER:

>> uparse [10 20] [gather [emit x: integer!, emit y: integer!]]
== make object! [
    x: 10
    y: 20
]

But what if the field's name is dynamic (maybe even captured from the parsed material itself?) Well, SET-GROUP! seems the obvious answer, doesn't it?

>> word: 'xxx

>> uparse "a" [gather [emit (word): <any>]]
== make object! [
    xxx: #a
]
2 Likes