REBOL Technologies

DEFAULT good and evil

Carl Sassenrath, CTO
REBOL Technologies
28-Jan-2009 0:06 GMT

Article #0388
Main page || Index || Prior Article [0387] || Next Article [0389] || 20 Comments || Send feedback

The R3 team has been investigating various useful idioms for inclusion into the mezzanine.

One idiom of interest is default. It would set a value if it's not already defined. It's a replacement for this common expression:

unless value? 'word [word: value]

We've discussed various usage forms...

word: default 'word value
default word: value
default [word: value]

The advantage of these is that the set-word gets expressed (so context gets properly established.) That's something you want to keep in mind in R3.

Of course, we knew that we could not write the minimal expression:

word: default value

Right? Or... could we?

It suddenly dawned on me that it could be written! Here it is:

default: func [val /local word] [
   word: back stack/block 1
   if set-word? word: word/1 [
       either any [not value? word none? get word][
           set word val
       ][
           val: get word
       ]
   ]
   val
]

Not really very pretty, but functional.

It would work like this (where abc is not yet defined in context):

>> abc: default 20
>> ?? abc
== 20

>> abc: default 0
>> ?? abc
== 20  ; correct! It's already defined

However, Carl's first lesson of post-modern computing is just because we can, doesn't mean we should. CAN leads to complexity, often for complexity's sake alone (from my second lesson of PMC: complexity begets complexity.)

So... what say you? Is it good? Evil?

Dust off that Ph.D. wall ornament, and really think about it.

20 Comments

Updated 9-Mar-2024   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code