||= is much needed?

Wes Garland wes at page.ca
Wed Jun 13 04:30:41 PDT 2012


On 13 June 2012 03:45, T.J. Crowder <tj at crowdersoftware.com> wrote:

> Is the reason for using ?: rather than ?? because we may want it for my
> desired second ternary? E.g., from my other message:
>

Something else to consider; if we use (a ?: b), it would have to be
equivalent to (a ? a : b) with the exception that a is only evaluated once,
just as it is in gcc.  Any other meaning would be confusing to end users,
due to the strong resemblance to the ternary operator.

The use-case here seems to be that it would have the value a whenever a is
undefined, not when a is truey.  This is a tricky enough difference that I
think it mandates a different operator, like ??.

That said, I don't think I have ever seen code like that in the field,
where the caller sorts out the "default values" instead of the callee.*

* later edit: I thought this was true, but see that I have been unwittingly
using this idiom myself for years, see  examples below.


> Very much looking forward to ?: (however we spell it) and ?=.
>

Me too. They are all over my C code and Makefiles.

Random usage (food for thought):
memcpy(newHnd->buffer, thisHnd->buffer, min(length, newHnd->length ?:
length));
const char *filename = gpsee_programRelativeFilename(cx, report->filename)
?: "<unknown filename>";
JS_SetGCParameter(rt, JSGC_MAX_BYTES, (size_t)strtol(rc_default_value(rc,
"gpsee_gc_maxbytes", "0"), NULL, 0) ?: (size_t)-1);

So, as you can see above, I tend to use ?: to supply default behaviour to
functions when necessary, particularly when they are library functions out
of my control.

GPSEE_SRC_DIR ?= $(shell pwd)
GREP          ?= grep
EGREP         ?= egrep
MAKEDEPEND    ?= gcc -E -MM -MG
NCURSES_LIB_NAME   ?= ncurses
BIN_DIR            ?= $(GPSEE_PREFIX_DIR)/bin

I use the ?= idiom quite heavily in my Makefiles as we don't use autoconf.
We simply have a configure script which generates an include file of "stuff
that's different for this platform", which gets included before the stuff
that's assigned with the ?=.   Also, the ?= allows the developer to
override particular variables from environment variables passed to make,
i.e. to install into a different target directory, use a different version
of gcc, etc.   This is idiom is especially powerful with Make's late
evaluation of variables, but I don't think we should go there in ES.

Wes

-- 
Wesley W. Garland
Director, Product Development
PageMail, Inc.
+1 613 542 2787 x 102
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120613/c093b73e/attachment.html>


More information about the es-discuss mailing list