less-docs icon indicating copy to clipboard operation
less-docs copied to clipboard

About Variables / Default Variables

Open HalfLegend opened this issue 5 years ago • 1 comments

http://lesscss.cn/features/#variables-feature-default-variables It writes: We sometimes get requests for default variables - an ability to set a variable only if it is not already set.

This feature is not required because you can easily override a variable by putting the definition afterwards.

For instance:

// library
@base-color: green;
@dark-color: darken(@base-color, 10%);

// use of library
@import "library.less";
@base-color: red;

This works fine because of Lazy Loading - base-color is overridden and dark-color is a dark red.


I don't think the behavior in the example meets the instruction, because the @base-color is already set to green, it shouldn't be reset to red.

So I think the instruction should be: " We sometimes get requests for default variables - an ability to set a variable only if it is not already set.

This feature is conflicted with 'overriding a variable by putting the definition afterwards', so we couldn't implement the behavior of default variables."

HalfLegend avatar Jun 23 '19 10:06 HalfLegend

so we couldn't implement the behavior of default variables."

Well, we could. It just would make no sense in Less. For more details on this see https://github.com/less/less.js/issues/1706

I don't think the behavior in the example meets the instruction, because the @base-color is already set to green, it shouldn't be reset to red.

I'm not sure I understand what you actually mean with this. Could you elaborate? Note that Less overriding is not a replacement for Sass !default. It's a different paradigm to achieve a desired behavior for similar use-cases (in other words: !default is a kludge required in Sass to achieve the things naturally existing in Less by design. Nothing more than this).

seven-phases-max avatar Jun 23 '19 11:06 seven-phases-max