-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable/List merging/concat functions and language features #2564
Comments
This is simply impossible because of declarative nature of the language. Less variables are actually immutable and stateless (i.e. "constants" in short), so (by design) you cannot modify the value of a variable but can only redefine/override it with a new variable of the same name. For the same reason you cannot do anything like
I'm afraid things like "felt" are not quite something that can become a rationale to put a feature into the core. Either way for list/array manipulation functions see Regarding of the property merge feature, strictly speaking its syntax is a direct violation of CSS and Less "declarativness", i.e. the property merge statements are clearly imperative. Fortunately this is not a critical defect (yet) because CSS properties are currently "write-only" and every non-duplicated property/value pair should appear in the CSS output (yet another help there is that `property: value` and `property+: value` statements create two independent property "layers"). But for sure this will become a problem when/if we decide to implement #2433 or so. |
I'm not closing this ticket yet (just in case anyone wants to express an opinion on the particular syntax). But honestly, aside from the special syntax, this is really no more than just a duplicate of #2467 and if you believe there's some reason for such feature to be implemented via some dedicated syntax rather than just through trivial functions, please express your ideas in #2467 (closed issues/feature-requests can be reopened at any moment). And in general the current approach is:
|
@seven-phases-max How would that reflect on #2433? That is, if we could treat properties like variables, does that mean it would conflict with property merging? It seems not quite right to say it's impossible, but just that it's difficult with the current code? |
I would not say it's imposible or even difficult to allow/implement at all. It will just create a weird mix of imperative and declarative stuff that pretty hard to follow with LL and LDW in mind (i.e. "a language defect"), e.g.: // less:
div {
p+: 1;
p : 2;
p+: 3;
x : property(p);
}
// -> css:
div {
p: 1, 3;
p: 2;
x: ?; // p is 2 at this point, but did not we append 3 in the end?
} In other words |
Actually, your example seems straightforward to me. Less specifies that properties are merged only if both are marked as mergeable. I don't know what current behavior is, but the following would be my expectation:
If the output is as you just specified (I just checked, and it is, yikes), this is an error in implementation. The That is, the last declaration of
Now I would consider an argument that the final value could be
As to this:
Maybe. There isn't 100% consensus on the categories or differences between those two terms. But, yes, in general I think we try to implement things based on "traditional declarativeness". In this case, however, pragmatism wins. The nature of CSS (mostly a problem of CSS3), combined with how Less defines mixins, means that authors inevitably have encountered situations of wanting to declare "optional partial overwrites" of declarations, since CSS3 defines some "shorthand" syntax for which there is no way to separate into individual longhand declarations, which breaks with the CSS 2.1 model. Merging is a legitimate practical solution and meets Less's core directive, which is to make writing and managing CSS easier. Less's core directive is not to make sure all features pass a declarative test, even though it's important to do as much as possible for consistency. As far as this feature, if we addressed that bug and quirk of Less regarding properties, then this request in relation to variables would, I think, make more sense. Meaning, it would make it clear that properties end up with only one value, (and don't move), so incorporating variables into similar declarations wouldn't seem as far-fetched. |
Yes, thinking about it more, the final value should probably be
|
There's reason to leave non identical property values in the output: div {
background: rgb(200, 54, 54); /* Hello IE6 etc. */
background: rgba(200, 54, 54, 0.5);
} |
Thanks for pointing out As far as #2467, I apologize for it looking like a duplicate issue. I actually didn't notice the proposed
Two comments:
I understand that you're trying to create a plugin system, but that is only going to make it more difficult for open-source project owners. Their downloaders have to download an extra (external) plugin and figure out how to implement it (in the case of grunt/gulp/etc). |
(upd., oops, sorry I always forget to mention a recipient) @kevinmartin
They do require Autoprefixer now, so
Properties are evaluated by a browser (so Less does not care of any cascading in this regard except certain specific cases) and the CSS has no scope. E.g. taking you first basic example: @base: g h i;
.special-mixin();
.special-mixin() {
@base+: m n o;
} It won't work even w/o
Because this will mean either an infinite recursion (when LDW and LL apply) or break existing behaviour (if LDW is disabled for such syntax). E.g.: @v: 0;
div-0 {
something: @v;
}
div-1 {
@v+: 1;
something: @v;
}
@v+: 2;
div-2 {
something: @v;
}
@v: boo!; What result would you expect for such code? |
Agh, damn it, of course. That said, the output of the property merge still seems a plainly obvious bug in that the merged result position is at the first property position and not the last property position, but of course that's tangential to this issue. As to this issue, you're right about imported variable scope and recursion in your example. While properties could possibly be referenced like variables, they still would not behave like variables in the sense that they are not inherited into the scope (which is an important distinction to make on #2433). It didn't compute that both could be referenced, but property combinators wouldn't be possible for both (which is why I brought up property referencing since the distinction between properties / variables was being asked for implicitly in this issue). Thanks for taking the time to explain. I'm satisfied that this can be closed. You're right that variables have a certain built-in behavior of "replacement in scope", whereas properties "output as is" and even duplicate. So merging of vars would have unexpected behavior based on design. Let's close. |
No, you don't. Less 2.5.0 supports loading additional functions inline via the new The format in which such a plugin has to be written is slightly different though, meaning you may end up having to spend a bit of time refactoring a plugin written for commandline consumption. (Full disclosure: I actually wrote the |
closing due to lack of support (and I agree) |
I was looking through the feature documentation, and I noticed we can easily merge properties, but not variables.
I noticed #2467, but I felt like this should be part of the core language. Some examples:
While creating my own mixin to simulate Example 3, I found a kind of discrepancy...
The suggested core language improvements and functions could definitely help clarify/fix/prevent the above issue and allow the Examples to executed.
The text was updated successfully, but these errors were encountered: