Comments on: Calling all idioms!
I want to add some common idioms to R3.
Idioms are kind of like the salt you add at the end of the stew. You don't want to add the salt too soon, because you need to taste the flavor first.
I've held back on adding idioms over the years, because such things are not very useful unless they get documented. Now that we have the DocBase wiki, that documentation can be added and maintained.
Also, idioms don't take much space, and they enhance the language with a standard, meaningful vocabulary.
But, in order to qualify, the idiom must be short, clear, useful, and have a good name. If I don't understand the code, it won't get added. And, if a good name cannot be found, then maybe the idiom isn't such a good one?
As an example idiom, here's a pattern many of us write fairly often when we write included script files:
unless value? 'fee [fee: 10]
The line says: if fee is not already defined, then give it a value. It wants a default value for a variable.
The idiom could be:
default fee 10
Yes, we've talked about this one before.
The definition would be:
default: func [
"Sets word to a default value if not already set."
unless value? word [set word value]
Ok, if you have a favorite, you can post it using the R3 CHAT command under #754 (R3/Idioms). If you do not have R3, watch the website over the next day or so. If you cannot wait, or do not plan to try R3 soon, then you can post it here in the comment section or on one of the many AltME worlds where someone can relay it to the R3 team.
idea is nice but do we need another reserved word in global context? "default" is quite popular name for variable.
fee: any [fee 10]
If the module system allows for renaming on import you won't have to worry about stuff like this.
Modules make the your problem go away, just me - there are no more reserved words in REBOL 3, even to the limited extent there ever were in REBOL.
I like this DEFAULT function, though it should also set the word if its value is NONE before, to handle the common case of function parameters made optional using refinements.
I don't know if this suggestion is what you are thinking about, but a default value being set, needs to be easily reset to the default.
Ie, "reset fee" ideally resets the variable to its default values, anyhow something better than "fee: fee/default-value"
For the penalty of keeping copies of values adding in "reset" (first setting the value to the refinement "fee/default-value" then "reset fee" = "fee: fee/default-value"
I am too much of a newbie to have any opinion whether the extra clutter is worthwhile, theoretically I can see a bonus of working with the one word to get things done to make the script clearer and simpler to read. My usual fix which is to keep the default values separate and reassign them is not elegant.
Greg, what you are talking about is much higher level than default values in the REBOL language. In REBOL all variables are by default either unset (for globals and object fields) or none (for everything else).
You are talking about keeping track of what the default value is supposed to be after the variable was set. We are talking about figuring out whether the variable has been set at all, and then choosing a different default than just unset or none.
|Peter W A Wood|
If these idioms are all going to be mezzanine functions, it would make sense for them to be collected in a module that is optionally loaded and not added to the Rebol core.
In fact, it would be ideal if all the mezzanines which are designed to make coding quicker (e.g. to-string, to-integer, func, function, context) were moved to optionally loadable modules.
This would then give people the choice of running a really lean Rebol.
I don't like the idea of modules for commonly used words. It'd mean having to include some of them with most every script you write, making even simple scripts look a bit cumbersome. And it adds another layer to the learning of REBOL - namely which module is word X in.|
Thanks Brian, thought I was probably off track.|
Post a Comment:
You can post a comment here. Keep it on-topic.