Boolean shortcuts

Herby Vojčík herby at mailbox.sk
Wed Jan 4 04:58:16 PST 2012


Curse that eval-uation of a code block! This prevents not only this, but a 
lot of more possible enhancements/reforms!

Also `{ !window }.foo` would be problematic since it will be pronounced 
code-block before . is reached. :-(

Thanks for pointing out, Herby

p.s. It is really wild. In node.js, eval("{}") returns {}, eval("{;}") 
returns undefined, eval("{;} 3") returns 3, but eval("{;} {}") returns 
undefined (why not {}? Or why did not first eval returned undefined?); 
eval("{;} ({})") returns {}, of course.

-----Pôvodná správa----- 
From: François REMY
Sent: Wednesday, January 04, 2012 1:26 PM
To: Herby Vojčík ; es-discuss at mozilla.org
Subject: Re: Boolean shortcuts

Doesn't play nice with already existing blocks :

    eval("{ !window }") // returns false;

Regards,
François

-----Message d'origine----- 
From: Herby Vojčík
Sent: Wednesday, January 04, 2012 12:49 PM
To: es-discuss at mozilla.org
Subject: Boolean shortcuts

(original article in
http://blog.herby.sk/blosxom/Programming/ES-next/boolean-shortcut.html)

Hello, what about shorter way to express booleans?

Booleans are very nice concept, but can get bulky. That is why people may
avoid using it, sometimes. Moreover, booleans are often put inside nice
"traity" names like isRetired or done which are nice in constructs like if
(!retired) {...}. It would be nice if they could be declared using shorter,
more natural way (more descriptive) instead of using explicit true and false
value assignment (imperative). Booleans have some instrinsic non-imperative,
descriptive feel, so I'd like to be able to express it.

In literals. In literals, let it be possible to use not only foo:true and
baz:false productions (comma-terminated except the very last one), but also
simplified foo and !baz productions. This will allow for example this nice
thing:

Object.defineProperty(foo, "id", {
  value: "Moo",
  !enumerable, configurable, writable
});

By this I have stolen ! character from Allen Wirfs-Brock's use in literal
extension (
http://wiki.ecmascript.org/doku.php?id=harmony:concise_object_literal_extensions
), but I believe ! is associated with boolean not and this use would be
more fit for it. Literals created this way (if booleans have "traity" names)
are shorter and naturally readable, so if this would be accepted for
literals only, it would be nice. Though I can envision the use of this in
the rest of boolean-value-assignment contexts, like:

In variable declarations. In variable declaration it would be nice if
instead of var done = false; it could be simply put that var !done;. I am
not proposing this for general assign (it would be awkward), only for
initialization in declaration.

This brings a sad assymetry that I can not use similar streamlined var
soFarSoGood; instead of var soFarSoGood = true;. false have its shortcut
initalization, and true does not? I do have a workaround proposal for this,
though it it less nice, but it is an idiom known to all Javascripters with
some experience. Let us allow use var !!soFarSoGood; as well. It can then be
used as a shortcut true if developer sees it fit for the situation.

The above paragraphs should of course hold for let and const declarations,
as well.

In classes. In classes (that is, in their prototypes, which are outlined by
class {...} block) the use of booleans is not very common, but for the sake
of orthogonality and not creating special cases (it is a curly block, too,
same as object literal and code block), let it be used in public declaration
akin to var above. This can allow for example creating Smalltalkish
isDuckType constructs like this:

class Animal {
  public !!isAnimal;
  ...
});

class Ape extends Animal {
  ...
});

class Human extends Ape {
  public !isAnimal;
  public !!isPerson;
  ...
});

(not that it is a great use-case, but just to show there is some; I would
propose it even if I knew of none, not to create differences between {...}
constructs.)

Herby

_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list