Having primarily used SASS when writing CSS, I am now preparing to try out LESS as well. Having a read through the docs, I'm confused with the following in the context of guarded mixins:
Additionally, the keyword true is the only truthy value, making these two mixins equivalent:
.truth (@a) when (@a) { ... } .truth (@a) when (@a = true) { ... }
Any value other than the keyword true is falsy:
.class { .truth(40); // Will not match any of the above definitions. }
Why will truth(40)
not match the first mixin? Doesn't when (@a)
simply state that only match the mixin if a value (any value) is given to @a
, essentially, if @a
exists? And since 40
is a value that exists, why would it not find a match in the given mixins?
For Conciseness...
...I think. It appears to be assumed by the programmers of LESS that if you do not want a guard on the mixin, then you of course put no
when
statement at all:This mixin is called regardless of the value of
@a
, but does require a value for@a
, and so is checking by default for "existence" of a value. So by the adding of awhen
guard, they have assumed one does indeed intend a further guard (and not just to allow any value), and so......makes for a shorthand way (LESS is about trying to speed coding) of writing the guard statement...
This makes for faster coding of a mere true/false switch for a guard, and probably seemed to be good sense over what would otherwise be a redundant statement if it instead evaluated the
when (@a)
astrue
for any value.It is, admittedly, a bit less intuitive (no pun intended) from a typical programming viewpoint where it would seem that if
@a
exists (as in, anything other thanfalse
,null
, or0
) then the phrasewhen (@a)
would evaluate totrue
. But instead of checking for "existence" of a value, they have made that check an explicit check for a passed value oftrue
(their only "truthy value" as far as guards are concerned).I don't have any documentation to back up my statements here, other than the statements on lesscss.org about how they have coded it, which state nothing about why they coded it to work that way (that part is my speculation at present, unless I can track down documentation of discussion about it).