You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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} 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])
@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:
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.
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 :(
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:
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?)
... 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?
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
}
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.
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.
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.
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.
@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.
@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)...
@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.
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.
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.
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.
Activity
goto-bus-stop commentedon Feb 4, 2014
👍
lukeapage commentedon Feb 4, 2014
yes. I am not against them.
We could implement them as they used to be implemented when they were part of less. see #6 !!
lukeapage commentedon Feb 4, 2014
though I'd like to see some thought about the syntax. possibly only allowing on
#idnot.id.just throwing out some other syntaxes
goto-bus-stop commentedon Feb 4, 2014
@{#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])lukeapage commentedon Feb 4, 2014
yes to b clear I'd suggest the first 2 depending on whether it was an interpolated format or not (e.g.
the grammer being
@#recognised as a special case and extracting from#until>as a 'namespace' followed by the variable name.matthew-dean commentedon Feb 4, 2014
+1 to
@{#ns>size}. Makes sense.@width: @#ns>sizelooks 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:Especially during interpolation:
.myselector .parent@#ns > childVar > .childElSoviut commentedon Feb 4, 2014
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.You're referencing the namespace with
#then using the>accessor to access the@sizevariable. Without the@next to the variable I worry that the syntax gets confusing since it starts looking like an element selector.lukeapage commentedon Feb 4, 2014
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 :(matthew-dean commentedon Feb 4, 2014
I don't think it necessarily puts
@{}where they wouldn't be already. I actually think both of these could co-exist: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 > @sizemay only work because it's written without any surrounding functions or other value data. Compare: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?)Soviut commentedon Feb 4, 2014
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.
matthew-dean commentedon Feb 4, 2014
Agreed. Otherwise libraries would wrap vars in namespaces, but you wouldn't be able to "configure" the library.
seven-phases-max commentedon Feb 18, 2014
(crosslinking the topic where this feature and the syntax emerged before: #1357, just for reference).
seven-phases-max commentedon Feb 18, 2014
Just a thought on
#ns > @varvs.@#ns > varWe 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:
Yep, a bit restrictive and makes the selector interpolation case verbose, but if it makes the syntax less confusing in other situations... why not?
matthew-dean commentedon Feb 18, 2014
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:
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:
matthew-dean commentedon Feb 18, 2014
@seven-phases-max I think your idea is valid, or we could just opt to not switchup the syntax within the interpolation syntax.
On the one hand, it's possibly one extra @, but at least everyone would know what the heck you were referring to.
66 remaining items
matthew-dean commentedon May 2, 2015
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.seven-phases-max commentedon May 2, 2015
So you mean you don't need a hypothetical
eachto invoke an arbitrary parametric mixin as its callback? ;)matthew-dean commentedon May 2, 2015
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: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.
calvinjuarez commentedon May 3, 2015
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.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 thinkparse-selector()or a whole other syntax would be better.matthew-dean commentedon May 3, 2015
@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.
calvinjuarez commentedon May 7, 2015
👍 sounds good. Sorry about that.
matthew-dean commentedon May 7, 2015
No need to be sorry. Hey, if at the end, we get something cool done, it's all good. 😄
rjgotten commentedon May 11, 2015
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)...matthew-dean commentedon May 11, 2015
@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.
calvinjuarez commentedon May 19, 2015
@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).matthew-dean commentedon May 19, 2015
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.chharvey commentedon Aug 4, 2015
this feature would be very similar to maps in Sass. hopefully the syntax for retrieving a value will be better than
map-get().matthew-dean commentedon Jan 16, 2016
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.
matthew-dean commentedon Jan 31, 2016
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.