Skip to content

Variable / property namespacing #12

Open

Description

@matthew-dean

The discussion around namespacing had some proposals on syntax, but I'm starting to think less/less.js#2767 makes the proposal problematic. less/less.js#1848

Yes, that was part of the namespacing discussion. However, after some of our discussion around unifying mixins and detached ruleset behavior, I'm not sure the syntax really makes sense.

Namespacing

For example, we were talking about:

val: @{#ns var};
val: ${#ns prop};

...with var and prop a variable and property inside that namespace, and the @ and $ applying to the last identifier. But if we make a detached ruleset a kind of (anonymous mixin assigned to a var), then this logic begins to quickly get confusing.

val: @{@dr var};  // which one does @ belong with?

We also talked about the short form #ns@var, which becomes something like @dr1@dr2@var, which conflicts with other syntax.

So, I'm thinking for 3.0, we re-think it to one of the earlier suggestions by @lukeapage which was close to the original Ruby syntax with [] for properties:

val: #ns[@var];
val: #ns[prop];
val: @dr[@var];
val: @dr[prop];

That places the @ symbol closer to the actual variable, and makes the identifier it belongs with less ambiguous.

Interpolation

... Discussion moved to #13, as it's not necessary to do both at the same time, or at all.

Activity

  1. matthew-dean commented on Jan 30, 2016

    @matthew-dean
    MemberAuthor

    By the way, a way to think about property / variable accessing is basically, this:

    foo: #ns[@bar];
    

    is sugar for this:

    & {
      #ns();
      foo: @bar;
    }
    

    (Unless we want to get clever with how we select which var values are valid)

  2. seven-phases-max commented on Jan 30, 2016

    @seven-phases-max
    Member

    There's one more important thing I guess we forgot to discuss earlier.
    When accessing object member we usually need both direct and indirect access, i.e. using JS-semantics:

    obj["ident"]; // -> obj.ident
    obj[var];   // -> obj.ident-in-var
    

    At quick glance it's not that difficult to target this for var fields using "indirect variable referencing" syntax as the base, e.g.

    @var: foo;
    val: #ns[@var];  // -> ns.var-variable
    val: #ns[@@var]; // -> ns.foo-variable
    

    but then:

    @var: foo;
    val: #ns[@var];  // -> ns.var-variable
    val: #ns[@@var]; // -> ns.foo-variable
    val: #ns[prop];  // -> ns.prop-property
    val: #ns[??];    // -> ns.foo-property
    

    So we need more ideas.

  3. matthew-dean commented on Jan 30, 2016

    @matthew-dean
    MemberAuthor

    @seven-phases-max Are you just talking about:

    @var: foo;
    val: #ns[$@var];    // -> ns.foo-property
    

    If we do @@var, then $@var seems like the logical extension of that.

    Also, I guess that could include variations of $$prop and @$prop although I'm not sure the use-cases, unless it's essentially because you are using properties as vars from a namespace?

  4. seven-phases-max commented on Jan 30, 2016

    @seven-phases-max
    Member

    If we do @@var, then $@var seems like the logical extension of that.

    Yes, but then should not we also require val: #ns[$prop] instead of #ns[prop]?
    Otherwise a common mistake will be

    @foo: bar;
    val: #ns[bar];
    val: #ns[@foo]; // oops, I thought that means #ns[bar] too
    
  5. seven-phases-max commented on Jan 30, 2016

    @seven-phases-max
    Member

    Now, after thinking of it a bit more,

    @foo: bar;
    val: #ns[bar];
    val: #ns[@foo]; // oops, I thought that means #ns[bar] too
    

    Do we really need to be that strict there at all?
    Can't we make #ns[ident] to match both property and variable (withwhatever reasonable precedence)? Then:

    @foo: bar;
    val: #ns[bar]; // can mean both #ns.bar-property and #ns.bar-variable (whichever happens to be there)
    val: #ns[@foo]; // means the same thing (and then we retain the usual `@foo` semantics).
    

    Sure it's about "guessing a user intention, bla-bla-bla" - but can we imagine any practical snippet where you have:

    #ns() {
        @foo: 2px;
        foo: @foo * 3;
    }

    and really want to have an access either to #ns[@foo] or #ns[$foo]?.
    I.e. would a supposedly minor ambiguity (and limitation of course) be worth a more clean syntax?

  6. seven-phases-max commented on Jan 30, 2016

    @seven-phases-max
    Member

    And yet another problem I did not want to mention before because it's also quite a story on its own.
    I already mentioned that currently namespaces are not "open" to each other. And this actually introduces quite dramatic problems with "overriding" in a longer run.

    Let's start with this snippet:

    is sugar for this:

    & {
      #ns();
      foo: @bar;
    }
    

    Now let's try some basic overriding:

    #ns {
      @bar: 42;
    }
    
    usage {
      #ns();
      foo: @bar;
    }
    
    #ns {
      @bar: 43;  
    }
    

    ^ works like a charm.

    Now something more close to practical use-cases:

    #ns {
      @bar: 42;
      @baz: @bar * 2;
    }
    
    usage {
      #ns();
      foo: @baz;
    }
    
    #ns {
      @bar: 43;  
    }
    

    Fail... :( (foo is 84 and not 86 as we would expect it to be for proper overriding purposes). The result is somewhat correct in fact, since when each #ns mixin is evaluated, its local variables have higher priority (or in other words, two #ns mixins are evaluated independently, thus while the second do override @bar within the usage scope, it does not override it in the first #ns scope).

    That's something we'll have to target too. (Note that we cannot change mixin evaluation behaviour in this case - reason).

  7. matthew-dean commented on Jan 30, 2016

    @matthew-dean
    MemberAuthor

    Yes, but then should not we also require val: #ns[$prop] instead of #ns[prop]?
    Otherwise a common mistake will be

    @foo: bar;
    val: #ns[bar];
    val: #ns[@foo]; // oops, I thought that means #ns[bar] too
    

    I don't see how that would be "common". In [] we're specifying what's being returned, so I probably should have said that #ns[@@foo]] is illegal as well. There's no such variable name as @@foo, which means we can probably ditch @$ $@ combinations, because the use case needs begin to drop steadily.

    Can't we make #ns[ident] to match both property and variable (withwhatever reasonable precedence)?

    At first I didn't know what you meant until I read it. And then it makes sense and yes, does increase the simplicity of the syntax.

    #ns[ident]  // either prop or var name
    #ns[@var]  // specifically a var name
    #ns[$prop]  // specifically a prop name
    

    I think that's a smart observation, that in most cases, it won't be ambiguous for the user which type of value they wish to return, as long as we document precedence rules.

    And yet another problem I did not want to mention before because it's also quite a story on its own.
    I already mentioned that currently namespaces are not "open" to each other. And this actually introduces quite dramatic problems with "overriding" in a longer run.
    {example 2}
    The result is somewhat correct in fact, since when each #ns mixin is evaluated, its local variables have higher priority

    I think what you mean is the evaluation order, no? I don't think it's intended to be "local variables win" (and I hope it's not documented that way) so much as mixins are evaluated and then merged. So a user to expect otherwise is to not know that mixins are evaluated before returning (even though that's consistent with other mixin behavior?)

    So, yes, namespaces are not open to each other. However, Less already does this kind of "referential collapsing". That is:

    #ns {
      .mixin() {
        one: two;
      }
    }
    #ns {
      .mixin() {
        three: four;
      }
    }
    usage {
      #ns.mixin();
    }
    

    The contents are not "open to each other". One reference is given, but both are called, evaluated, and returned. So, no, there's not one namespace. Whenever you referencing a "namespace" you're calling a collection. The same would be true with properties and variables.

    #ns {
      @value: original;
    }
    #ns {
      @value: overridden;
    }
    usage {
      #ns(); 
    }
    

    The second #ns isn't really overridding the first variable. It's just effectively returning:

    usage {
      @value: original;
      @value: overridden;
      value: @value;
    }
    

    So it's just straightforward Less evaluation. It's evaluating the mixin, then returning the variable. So this:

    #ns {
      @value: original;
    }
    #ns {
      @value: overridden;
    }
    usage {
      value: #ns[value]; 
    }
    

    is effectively

    usage {
      & {
        #ns();
        value: @value; 
      }
      @foo: @value;    // Error: unknown variable name "@value"
    }
    

    Make sense? Nothing new is changing in behavior, just a more convenient syntax. You're collapsing 4 lines into one.

  8. seven-phases-max commented on Jan 30, 2016

    @seven-phases-max
    Member

    It's just effectively returning:

    usage {
      @value: original;
      @value: overridden;
      value: @value;
    }
    

    That's what I mean, if you change the example to

    #ns {
      @value: original;
      @derived: @value + 1;
    }
    #ns {
      @value: overridden;
    }
    usage {
      #ns();
      value: @derived; 
    }
    

    it's not anymore equal to

    usage {
      @value: original;
      @derived: @value + 1;
      @value: overridden;
      value: @derived;
    }
    

    I.e. for #ns[value]; to be usable we need first to collapse and then evaluate (while for mixins it's first evaluate then collapse).

  9. matthew-dean commented on Jan 31, 2016

    @matthew-dean
    MemberAuthor

    Oh, ok, that. But..... if someone is doing that, aren't they kind of idiotic isn't that weird? Why would you define a variable in a separate place and then try to pull a derived variable that's not in the same scope? Were there use cases that had those expectations? Sure, that specific use case might have an unexpected result, but... [shrug] ?

    I'm just trying to think why we would need to address that specific case. I don't think we need to try to solve all of Less's weird scope edge cases just to make the syntax more convenient for 99% of use cases.

  10. seven-phases-max commented on Jan 31, 2016

    @seven-phases-max
    Member
    #ns[ident]  // either prop or var name
    #ns[@var]   // specifically a var name
    #ns[$prop]  // specifically a prop name
    

    No, I actually did mean:

    #ns[ident]  // either prop or var name
    #ns[@var]  // still either prop or var (name specified by `@var` value)
    

    Then answer to:

    I don't see how that would be "common". In [] we're specifying what's being returned, ...

    would be the question "where did we specify that (OK, docs) and does anybody read that there?" :)
    I.e. as always they will use their JS-intuition where

    var foo = bar;
    val = obj[foo]; // -> obj.bar;
    

    thus they will expect

    @foo: bar;
    val: #obj[@foo];
    

    to do the same (and then we'll have "Less is not JS, bla-bla-bla, RTFM").


    But yes, if we won't agree on that my (quite radical, yep) variant, then:

    #ns[ident]  // either prop or var name
    #ns[@var]   // specifically a var name
    #ns[$prop]  // specifically a prop name
    

    is a good option.

  11. matthew-dean commented on Jan 31, 2016

    @matthew-dean
    MemberAuthor

    It's not quite the same as JS, because we're using unquoted identifiers. When we write this:

    #ns[ident]  
    #ns[@var]
    

    ...the equivalent in JS would be:

    #ns["ident"]  
    #ns["@var"]
    

    It's just that CSS/Less allows plain keywords, so quoting here is not consistent.

  12. seven-phases-max commented on Jan 31, 2016

    @seven-phases-max
    Member

    ...the equivalent in JS would be:

    It's again only because you know what it's supposed to be. But they know only that JS var in ns[var] is a variable (not an ns member identifier), hence for Less they'll think ns[@var] means the same. :)

  13. seven-phases-max commented on Jan 31, 2016

    @seven-phases-max
    Member

    Back to evaluation:

    Why would you define a variable in a separate place and then try to pull a derived variable that's not in the same scope?

    Actually it is one of the primary use-cases for the namespaces.

    // .....................................................
    // framework:
    
    #theme {
       @color: red;
       @background-color: #fff - @color;
       @secondary-color: lighten(@color, 25%);
       @alt-color: spin(@color, 45);
       // etc. etc.
    }
    
    .btn {
       color: #theme[color];
       background-color: #theme[background-color];
    }
    
    // .....................................................
    // user app:
    
    #theme {
        @color: blue;
        // here I expect all depended theme colors to change accordingly
        // (just like it *works* if I'd use global vars)
        // "Your namespaces are useless, guys! I'll stick to my global garbage" <- that's why it's important
    }

    P.S. There're workarounds as usual, but they are... well... workarounds (like this).

  14. matthew-dean commented on Jan 31, 2016

    @matthew-dean
    MemberAuthor

    Alright, that's possible lol.

    So, do you have a proposed solution?

  15. 47 remaining items

  16. matthew-dean commented on Feb 7, 2018

    @matthew-dean
    MemberAuthor

    @seven-phases-max

    Honestly? I always hated this @{} syntax the most in Less. I'd rather prefer #ns->identor #ns then. Sure, this is more like irrational hate (having just a bit of rationality for "too verbose", "too {}-bock-like looking), counting @{} is already there and nothing can be done with it. But, yet again to be really honest and whining, I'd really agree for any alternative instead (incl. conflicting #ns[ident] or ugly @ns|ident) :).

    Haha fair enough, and I came back today to be like, "uuuuugh do we REALLY have to wrap all namespaced properties"?

    For the same reason, .ns[[prop]] seems like an annoying level of wrapping.

    I never considered #ns<ident>. That's cool and nothing immediately strikes me as bad about it.

    Usage:

    .test {
      val: @dr<@var>;
      val: @dr<@dr><@var>;
      grid-template-rows: [main-start] .main<content> [content-start];
    }

    vs.

    .test {
      val: @dr|@var;
      val: @dr|@dr|@var;
      grid-template-rows: [main-start] .main|content [content-start];
    }

    🤔 The pipe | I wonder if it's almost too gentle of a separator, similar to your instincts. But on the other hand, multiple >< starts to look a little busy (although that's gonna be the more rare use case.

    🤔 🤔 🤔 I dunno, the pipe is kinda fucking cool. Very clean result. vs.....

    .test {
      val: @dr->@var;
      val: @dr->@dr->@var;
      grid-template-rows: [main-start] .main->content [content-start];
    }

    HMMMMMMMM... the explicit nature of -> very strongly conveys the semantic meaning of a retrieval here, and adds more clarity in the grid-template-rows value. And maybe the limitation of "don't end identifiers with a hyphen" (which, who would do that anyway?) is no big deal.

    And -> is more tied in languages to map access than | probably. Like, if we're not doing brackets [] then it's probably the #2 most used, no?

    So yeah, I would agree with all 3 of these options over ${ns[ident]}. These are good suggestions.

  17. matthew-dean commented on Feb 7, 2018

    @matthew-dean
    MemberAuthor

    Ha, I noticed after posting that Github did some good code coloring with <> and -> whereas pipes look kinda dead. This is minor, but enough to probably bump | more solidly to last place on that list of 3 for me.

  18. matthew-dean commented on Feb 7, 2018

    @matthew-dean
    MemberAuthor

    The nice thing about -> is that you can use legacy mixin syntax to do:

    .test {
      val: #ns > .mixin -> @var;
    }

    vs.

    .test {
      val: #ns > .mixin  <@var>;
    }

    That is, when space-separated, the latter starts to look more strange and inconsistent.

  19. seven-phases-max commented on Feb 8, 2018

    @seven-phases-max
    Member

    These -> and <...> were just random examples of course (I simply borrowed from C++) - I did not really considered them seriously. Sure, if we go deeper they reveal to be not very suitable, too ambiguous even if there's no direct conflicts:

    • .ns->ident - is it a member access or .ns- greater than ident?
    • .ns<ident> another - is it boolean (.ns < ident) > another?
      etc.

    So please do not treat these as some kind of proposal (they were really just random inspirational goodies to stress individual habits. :)


    That is, when space-separated, the latter starts to look more strange and inconsistent.

    That's another story. There're reasons (and this is one of them) why in either C-like language you (ideally) always write member-access and subscript operators w/o spaces. E.g. in JavaScript:

    let a =objects[12  ].  memberArray [ 42]
    ;
    

    is valid (if I'm not mistaken) but we never write it like this :) (it's always obj.member and array[index] thus the legacy #ns > .mixin-like stuff (when it's a namespace.member thing and not a CSS selector at all) is really a bogus formatting style).

  20. matthew-dean commented on Feb 9, 2018

    @matthew-dean
    MemberAuthor

    These -> and <...> were just random examples of course (I simply borrowed from C++) - I did not really considered them seriously. Sure, if we go deeper they reveal to be not very suitable, too ambiguous even if there's no direct conflicts:

    So please do not treat these as some kind of proposal (they were really just random inspirational goodies to stress individual habits. :)

    [sigh]

    Okay. Well... suggestions?

  21. seven-phases-max commented on Feb 9, 2018

    @seven-phases-max
    Member

    I still do like #ns[ident] thing the most so far. I was just wondering if we could invent something comparable considering the new [] baby of CSS. It seems we can't :)
    Technically I think if you've already started experimenting with implementation it's fine to go with [] and if necessary it can be changed to something else (#ns≥iden≤) even after RC.. I assuming the most tedious part there will be the evaluation code while the parsing part (at east what for what comes after #ns/.ns) is relatively easy (thus not so critical to later changes).

  22. matthew-dean commented on Feb 9, 2018

    @matthew-dean
    MemberAuthor

    I still do like #ns[ident] thing the most so far. I was just wondering if we could invent something comparable considering the new [] baby of CSS. It seems we can't :)

    lol @seven-phases-max sometimes it's hard to glean what your actual position is. 😉

    Okay..... if #ns[ident] works in the short term, let's do it. No, I haven't started any implementation and I'd really prefer to not do that work myself (unless I can finally convince my team at work to switch from Sass to Less, which.... no luck so far). Basically all I did was start a new branch, and check-in the tests that would need to pass based on that syntax.

  23. seven-phases-max commented on Feb 9, 2018

    @seven-phases-max
    Member

    lol @seven-phases-max sometimes it's hard to glean what your actual position is.

    :)
    Well, it's just like you start to think of it and come to a conclusion that if it's become too ugly/verbose or too conflicting then at some point you may find yourself preferring something like (already working code):

    div {
        color: some-ns(primary); // == .some-ns[@primary]
    }
    
    .some-ns {
        @primary:   red;
        @secondary: blue;
        // etc.
    }
    
    // map namespace to function using corresponding plugin:
    .function-some-ns(@ident) {
        .some-ns;
        return: @@ident;
    }

    which will be sad counting the efforts (to be) taken.
    So you're keep trying to find better ways (one would expect from the native impl.) till the very end.

  24. matthew-dean commented on Jun 29, 2018

    @matthew-dean
    MemberAuthor

    Please check out my PR! less/less.js#3242

  25. matthew-dean commented on Jun 30, 2018

    @matthew-dean
    MemberAuthor

    Question: could we not apply the concept of lookups to lists? That is, why not allow it as a shorthand for:

    @items: 1 2 3;
    @value: extract(@items, 2);  // old
    @value: @items[2];  // new

    I mean, the evaluation is really not that hard. Most of the work has been done. So, like JavaScript, it could refer to index-based or key-based lookups. The only confusion might be that I think Less lists are 1-based, not 0-based? But I think I would still keep it consistent with whatever extract() is doing.

  26. calvinjuarez commented on Jul 1, 2018

    @calvinjuarez
    Member

    Saying @itemsArray: one two three; is like an "Array" and @itemsObject: { @one:one; @two:two; @three:three; } is like an "Object" seems like a nice parallel to me. It might be a bit weird, but I can't think of any direct objection, except that we're adding a relationship between two features that weren't all that related. Though the relationship kind of exists, we'd just be formalizing and unifying it.

    I think I'd want to deprecate (¿or maybe just discourage?) extract() in that case, so the question doesn't become "Why can't I extract(@itemsObject, @one)?".

    Got my 👍, but I don't think it's super critical, unless it turns out to be low-hanging fruit.

  27. matthew-dean commented on Jul 1, 2018

    @matthew-dean
    MemberAuthor

    @calvinjuarez Well it parallelizes other languages like PHP and JavaScript which overlap arrays / maps / objects in the way they're accessed. So yeah, if we imagine every list as having a key/value, just that flat lists like one two three, the keys for that are just 1 2 3 (since Less lists are 1-based). So all lists have keys, we just introduce that some are their indices, and some are keywords / names.

    except that we're adding a relationship between two features that weren't all that related.

    They weren't related, until I started to use rulesets as maps for writing up 3.5 examples. Once you start to think of rulesets as just data, which may produce rules, but may be considered a map of values, then lists start to take on a semantically similar vibe.

    What I mean is, in Less, a "list" may just be a property value of a sequence of keywords, or it may be "used" as data for some Less functional purpose. In other words, Less already "re-purposes" CSS structure to produce "data".

    Trust me, start using 3.5 syntax and you'll see what I mean lol.

  28. matthew-dean commented on Jul 3, 2018

    @matthew-dean
    MemberAuthor

    Btw, I ran into this today related to this comment from @seven-phases-max.

    To summarize his comment: mixins technically merge their rules together, and that includes their variables. But the values of mixins get evaluated before merge. So even before 3.5, if you wrote:

    .mixin() {
      @value-1: 1;
      @value-2: @value-1;
    }
    .mixin() {
      @value-1: 2;
    }
    
    .foo {
      .mixin();
      bar: @value-2;
    }
    
    // output
    
    .foo {
      bar: 1;
    }

    This problem isn't resolved in 3.5, bar: .mixin[@value-2] would still produce 1 as a value.

    To be honest, I don't know that we want to change the behavior, especially with the goal of making vars private in the #16 discussion. But we may need to be careful to document that overriding a value for a variable will not change the computed value of other vars. Instead, users should be encouraged to evaluate the computed values at call time.

    I basically "re-discovered" this problem when tinkering with the Less library that I'm using to test out this feature.

  29. calvinjuarez commented on Jul 14, 2018

    @calvinjuarez
    Member

    @matthew-dean No, no, to reiterate, I have no objections. In my comment I'm stating essentially "Yeah, this all makes a lot of sense". I was thinking inline, though, so it's a little meandering. Sorry about that. I'm on-board.

    As to the value evaluation, I think that's a quirk and feature of Less. I'm not sure I'd want it changed.

    .mixin() { // default
      @var: foo;
    }
    .mixin() when (@i-want-var-to-be-bar) {
      @var: bar;
    }
    
    //...
    @i-want-var-to-be-bar: true;
    
    .selector {
      -less-show: .mixin()[@var];
    }
    .selector {
      -less-show: bar;
    }

    Right?

    Update:

    So, then,

    .mixin() { // default
      @var: foo;
      -less-show: @var;
    }
    .mixin() when (@i-want-var-to-be-bar) {
      @var: bar;
    }
    
    //...
    @i-want-var-to-be-bar: true;
    
    .selector {
      .mixin();
    }
    .selector {
      -less-show: bar;
    }

    They're essentially the same, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions