Skip to content

Public variables on namespaces #1848

Closed
Closed

Description

@Soviut

I realize this isn't a new topic but it I think the time has come to consider implementing public variables on namespaces. Many prominent libraries for LESS (bootstrap, hat, etc.), have emerged, each with dozens of configuration variables which could very well overlap and conflict with each other.

Currently, namespaces support private variables, the only way to get at them is via mixins within the namespace and those mixins can then be used externally; Sort of like a closure:

#ns {
    @size: 10px;
    .box() {
        width: @size;
        height: @size;
    }
}

.icon {
    #ns > .box();
}

Which yields:

.icon {
    width: 10px;
    height: 10px;
}

However, I think it would be very handy to do the following:

#ns {
    @size: 10px;
}

.icon {
    width: #ns > @size;
    height: #ns > @size;
}

As well as update those variables within the namespace:

#ns > @size: 20px;

Activity

  1. goto-bus-stop commented on Feb 4, 2014

    @goto-bus-stop

    👍

  2. lukeapage commented on Feb 4, 2014

    @lukeapage
    Member

    yes. I am not against them.

    We could implement them as they used to be implemented when they were part of less. see #6 !!

  3. lukeapage commented on Feb 4, 2014

    @lukeapage
    Member

    though I'd like to see some thought about the syntax. possibly only allowing on #id not .id.

    just throwing out some other syntaxes

    @#ns>size // could work interpolated e.g. 
    @{#ns>size}
    #ns > @size
    #ns>@size
    #ns['size']
    #ns[size]
    @#ns[size]
    #ns[@size]
    
  4. goto-bus-stop commented on Feb 4, 2014

    @goto-bus-stop

    @{#ns>size} seems to make the most sense to me. Since it kinda has to work with interpolation, using the existing @{} would be the logical way to go… Perhaps @{#ns[size]} or similar could work too but it's not very pretty. (e; actually it's not even that bad.) #ns[@size] is pretty but it's hard to interpolate (.grid-#ns[@grid-size])

  5. lukeapage commented on Feb 4, 2014

    @lukeapage
    Member

    yes to b clear I'd suggest the first 2 depending on whether it was an interpolated format or not (e.g.

    .grid-@{#ns>size} {
      @width: @#ns>size;
    }
    

    the grammer being
    @# recognised as a special case and extracting from # until > as a 'namespace' followed by the variable name.

  6. matthew-dean commented on Feb 4, 2014

    @matthew-dean
    Member

    +1 to @{#ns>size}. Makes sense.

    @width: @#ns>size looks potentially more problematic, since you're assuming no white space. I think @{ #ns > size } would be perfectly legal, and more readable, but suddenly gets weird here:

    @width: @#ns  >  size;
    

    Especially during interpolation:

    .myselector .parent@#ns > childVar > .childEl

  7. Soviut commented on Feb 4, 2014

    @Soviut
    Author

    The idea of prefixing the namespace with an @ symbol when dealing with a variable seems very strange to me. I personally think the following makes the most sense.

    @width: #ns > @size;
    

    You're referencing the namespace with # then using the > accessor to access the @size variable. Without the @ next to the variable I worry that the syntax gets confusing since it starts looking like an element selector.

  8. lukeapage commented on Feb 4, 2014

    @lukeapage
    Member

    The idea of prefixing the namespace with an @ symbol when dealing with a variable seems very strange to me. I personally think the following makes the most sense.

    I agree but this means it doesn't work in interpolated cases unless you have 2 differing syntaxes.

    I don't like @{#ns>size} everywhere because it puts curly brackets in even more places :(

  9. matthew-dean commented on Feb 4, 2014

    @matthew-dean
    Member

    I don't think it necessarily puts @{} where they wouldn't be already. I actually think both of these could co-exist:

    @width: #ns > @size;
    @width: @{#ns>size};
    

    The first, like @Soviut says, reads as "within the namespace #ns, reference the @SiZe variable" (and would be semantically in tune with namespaced mixins). The second reads as "within a variable context, reference size from the #ns namespace" (and would be semantically in tune with interpolation).

    However, I still feel like @width: #ns > @size may only work because it's written without any surrounding functions or other value data. Compare:

    .myElement {
       #gradient > .horizontal(darken(#ns > @color, 10%), #ns > @color);
    }
    .myElement {
       #gradient > .horizontal(darken(@{#ns > color}, 10%), @{#ns > color});
    }
    

    Hmm... it's actually hard to say which feels better. But... much as I've said on other issues, it might still suggest both could be correct? (To summarize, I believe @{} and @ references for vars should be more interchangeable. We already have two syntaxes to reference vars depending on placement, so why not two ways to reference a namespaced var?)

  10. Soviut commented on Feb 4, 2014

    @Soviut
    Author

    Keep in mind that we should be able to set/override variables within namespaces so we need the syntax to work with that as well.

    #ns > @size: 20px;
    
  11. matthew-dean commented on Feb 4, 2014

    @matthew-dean
    Member

    Keep in mind that we should be able to set/override variables within namespaces so we need the syntax to work with that as well.

    Agreed. Otherwise libraries would wrap vars in namespaces, but you wouldn't be able to "configure" the library.

  12. seven-phases-max commented on Feb 18, 2014

    @seven-phases-max
    Member

    (crosslinking the topic where this feature and the syntax emerged before: #1357, just for reference).

  13. seven-phases-max commented on Feb 18, 2014

    @seven-phases-max
    Member

    Just a thought on #ns > @var vs. @#ns > var

    ... but this means it doesn't work in interpolated cases unless you have 2 differing syntaxes.

    We may to not allow to use that in a selector interpolation at all. Just the same way as we don't allow any functions or arithmetic (or anything else except a simple variable) to be there. After all one can always write:

    @var: #ns > @size;
    .grid-@{var} {
        // ...
    }
    

    Yep, a bit restrictive and makes the selector interpolation case verbose, but if it makes the syntax less confusing in other situations... why not?

  14. matthew-dean commented on Feb 18, 2014

    @matthew-dean
    Member

    I just had this come up on a project, where I really really needed to namespace my variables, but also wanted to expose them. My solution:

    // core library
    #namespace {
      .vars() {
        @base-color: red;
      }
    }
    
    // custom project
    #namespace {
      .vars() {
        @base-color: blue;
      }
    }
    
    // core library implementation
    .box {
      #namespace > .vars();
      color: @base-color;
    }
    

    I'm assuming that one can, by design, do something like the above? The downside, of course, is needing to "import" colors into every scope that needs them available. But, basically, by "setting" variables via a mixin, then calling the mixin name in a scope, it has the desired effect.

    Which leads me to the idea that for setting variables, because of inheritance, it doesn't need a special "setting" format. It can be:

    #ns {
      @var: this;
    }
    
    // later, override
    #ns {
      @var: that;
    }
    
    //referencing:
    .foo {
      bar: #ns > @var;  // only referencing via >, not setting, just like regular namespacing
    }
    
  15. matthew-dean commented on Feb 18, 2014

    @matthew-dean
    Member

    @seven-phases-max I think your idea is valid, or we could just opt to not switchup the syntax within the interpolation syntax.

    #ns {
      @var: red;
    }
    .@{#ns > @var} {
      foo: bar;
    }
    .foo {
      bar: #ns > @var;
    }
    

    On the one hand, it's possibly one extra @, but at least everyone would know what the heck you were referring to.

  16. 66 remaining items

  17. matthew-dean commented on May 2, 2015

    @matthew-dean
    Member

    Right. It would go against what I quoted. So I'm saying I'm against it. That is, I would be against $() for selectors, because it would sit too close to ${}.

    And, I'm saying assigning selectors to variables seems like much less of a problem. I'm not sure we need it. So the potential future conflict against a desire to write $() seems like a non-issue at this point. I think it's a good direction to not mix returning references and returning values within the same syntax.

  18. seven-phases-max commented on May 2, 2015

    @seven-phases-max
    Member

    I'm not sure we need it.

    So you mean you don't need a hypothetical each to invoke an arbitrary parametric mixin as its callback? ;)

  19. matthew-dean commented on May 2, 2015

    @matthew-dean
    Member

    Touché. Not necessarily. I think, in that example, the assignment to a variable was just to allow the variable to be passed to the each(), but that's not a requirement. For instance, we don't require :extend() to surround the selector portion with another selector designation before the keyword. In other words, we didn't require:

    :extend(selector(#ns > something) all);
    

    Another way to put it, if we have a core function in which we're inputting a selector, we should find a way to pass that selector without specially wrapping it or assigning it to a variable, just for the purpose of passing it into the function. If we do, it's just extra syntax that we've forced ourselves to create.

    Yes, the outcome of this issue may shift a few of those other pending issues, but I think that's okay. I think this is the more valuable one to get right.

  20. calvinjuarez commented on May 3, 2015

    @calvinjuarez
    Member

    I don't like $() for selector parsing. It very plainly resembles the jQuery convention, which would be fine, except that jQuery's $() works very differently to how (I understand) the selector function is meant to. What Less needs (and what #1421 is about) is a way to assign a CSS selector to a variable, and not to access an existing entity. jQuery's $('.selector'), though, is all about getting an existing entity. So, if you've been in a jQuery file all day, and then you come across this, you're gonna have a conceptual collision.

    .selector {
      min-height: 1px;
      width: 100%;
    }
    
    @var: $(.selector);

    As such, to my mind, the similarity to jQuery is a big reason not to use $() for selector parsing, and instead to define something distinct and fresh.

    And speaking of distinct and fresh, I think the only symbols as "safe" as $ would be ^ or |. Both are also only used in CSS attribute selectors and don't (afaik) have a specific meaning in Less yet. Though if not $, I think parse-selector() or a whole other syntax would be better.

  21. matthew-dean commented on May 3, 2015

    @matthew-dean
    Member

    @calvinjuarez Agree for the same reasons. It looks like jQuery, but isn't. But, as I said, my recommendation is that we move that discussion to issue... #2270 ? And narrow the scope of this issue back to accessing within namespaces.

  22. calvinjuarez commented on May 7, 2015

    @calvinjuarez
    Member

    👍 sounds good. Sorry about that.

  23. matthew-dean commented on May 7, 2015

    @matthew-dean
    Member

    No need to be sorry. Hey, if at the end, we get something cool done, it's all good. 😄

  24. rjgotten commented on May 11, 2015

    @rjgotten
    Contributor

    @calvinjuarez
    And speaking of distinct and fresh, I think the only symbols as "safe" as $ would be ^ or |.

    What about &?

    It's already being used as a template token to indicate 'previous selector set' when used inside a selector declaration. Might as well make that go full-circle and create a &(<selector>) function to capture a (list of) selector(s) into a (list of) string(s)...

  25. matthew-dean commented on May 11, 2015

    @matthew-dean
    Member

    @rjgotten Can you move selector access discussion to #2270 or #2481 so that we can finalize this one (namespaced vars)? I think this is otherwise pretty close to ready.

  26. calvinjuarez commented on May 19, 2015

    @calvinjuarez
    Member

    @matthew-dean I think #1328 #1421 would be the best place for the selector parsing discussion (or a new, consolidated issue).


    For this thread, are there any objections to the following for namespaced variable accessing?

    @{#ns var} // and `@{#ns > var}` (w/ `#ns@var` as sugar in the next major version)
    

    Then this can be marked as "ready", and we can move the property access discussion (${#ns prop}) to #2433 (#76, #6).

  27. matthew-dean commented on May 19, 2015

    @matthew-dean
    Member

    No objections to those variants. I also think prepending @ to the var name inside the braces (as in @{#ns @var}) could be optional if it feels more logical to a particular developer. It doesn't change the meaning, so it seems nice as an option, in case they want to see the variable referenced as it was written within the namespace. But if there are objections to that form within this thread, I'm not attached to it. It was brought up at some point.

  28. chharvey commented on Aug 4, 2015

    @chharvey
    Contributor

    this feature would be very similar to maps in Sass. hopefully the syntax for retrieving a value will be better than map-get().

  29. matthew-dean commented on Jan 16, 2016

    @matthew-dean
    Member

    Despite this seeming close to final syntax, I think this syntax needs to be revisited, based on discussion in #2767 and less/less-meta#10.

    But not a total revisiting. @seven-phases-max's arguments in this thread toward #ns[prop] and @dr[prop] as aligning under a single behavior make sense is we move towards aligning all the behavior of rulesets in terms of scoping.

    That is, if you support DR variable access (which aligning ruleset behavior may allow), then @{@dr@var} starts to not make any sense.

    And @lukeapage's concerns about mixins introducing constants within scope might be lessened if we make scoping rules a bit more robust.

    In short, I would say this should be part of a larger syntax review in 3.0 (as part of the roadmap proposal) to make sure all these pieces together make sense.

  30. matthew-dean commented on Jan 31, 2016

    @matthew-dean
    Member

    As I mentioned in the comment above, this discussion got reset a bit because of changes that are part of some syntax and behavior clean-up discussions for 3.0. Because the discussion about syntax covers more than just this issue (but consistency with other 3.0 proposals), I'm going to close this issue in favor of the discussion here: less/less-meta#12.

    It would be great if anyone involved in this discussion could give their 2 cents on that issue.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions