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
{{ message }}
Repository navigation
Allow parametrized mixins as detached rulesets to form 'lambdas' #2270
Lambda mixin support would also neatly resolve recurring issues with function return arguments and the ugly hack where variables 'bubble up' to parent scope if said variables are as of yet undefined in said parent scope.
The suggested practice could become to adopt continuation style programming; passing 'return values' along into a lambda mixin to continue down the scope chain. This kind of mechanism is more transparent to users, less brittle by avoiding issues with potential variable name collisions and just fits in better with the overall functional programming paradigms that the LESS syntax is built on.
[EDIT]
Having just had a look at the way detached rulesets and calls are implemented in the AST, I think very little needs to happen to make this work. Even on the parser side of things, it seems fairly simple to just parse an optional block of mixin.args before blockRuleset in the detachedRuleset parser function and pass the arguments along to the tree.DetachedRuleset node instance. (The tree.DetachedRuleset would need to be extended with the params evaluation from tree.mixin.Definition, ofcourse.)
the ugly hack where variables 'bubble up' to parent scope
Just in case this is not ugly hack but well defined language facility.
(I also can't get the idea of solving "up-chain scope issues" with "down-chain parameter pass feature", these do not seem to be related, do they? But well, never mind, the feature remains useful regardless of this anyway)
Well, either way the idea itself is quite self-revealing and the only problem is ambiguous syntax, notice:
@a: {1: 1}; // OK, ruleset
@b: (@a); // OK, set @b to @a
@c: (@a) (@b); // OK, array/list
@d: (@a) {2: 2}; // ?!, array or unnamed ruleset with one parameter?
// same goes if this to be used as a mixin arg
In other languages with such facility this problem is solved with some specific keyword/operators preceding such lambda (e.g. [] in C++ or just function in JavaScript).
Just in case this is not ugly hack but well defined language facility.
When I mention it being a 'hack', I specifically mean that it seems hacked into the existing language desgin (to facilitate return values as a later addition), rather than something that was thought about up front. I don't mean the actual code is of low quality. Actually; the LESS parser and AST are quite well done. (Tip of the hat for that.)
However, the fact that it's well-defined and well-engineered doesn't make it less ugly when used. It's a language feature that is completely counter to intuition for most, as no other mainstream language does this.
the only problem is ambiguous syntax
In other languages with such facility this problem is solved with some specific keyword/operators preceding such lambda
May I suggest @a: (@b) => { b : b } ?
It's reminiscent of both C#'s lambda delegates and JavaScript's ES6 Harmony arrow notation for functions with preserved lexical scope. Provided you place detached rulesets at the head of the types the parser tries to apply for expressions (which I think it already is), this should result in a deterministic parsing without ambiguities.
And if you really wanted to have a list with an explicit => symbol in there, well; there's always ~"=>" as an escape hatch. (It's not like you don't already have this problem with calc() to some degree.)
rather than something that was thought about up front.
(Though we're quickly going offtopic - Yes, it was not something thought of the earlier Less days but today it still remains the only way to "return" something from something in Less (since the "function" facility is still somewhat neglected, though actually the whole thing is not limited to returning variables). So I insist :) that "parent scope leak" has nothing to do with lambdas, after all lambdas do not return anything at all. And the rest is another unrelated story).
@a: (@b) => { b : b } - Yes, could be something like this (though personally I extremely don't like => :)
However, there's no reason that you shouldn't be able to use the same construct for user-defined mixins: .for(@list, .mixin); and do whatever you want with the list / mixin parameters.
To be accurate, this is an extension of mixins / parameters, and not detached rulesets, and passing mixins by reference has been discussed elsewhere (although I couldn't find it with a brief search).
this now seems like an ideal candidate to close in favor of a user-defined function via @plugin
Note that currently you can neither pass identifiers like .mixin to a function (not allowed by the parser) nor you can call a function w/o assigning its result to something... So, no, for now it's simply impossible to implement such each via plugin.
So, no, for now it's simply impossible to implement such each via plugin.
Oh. Damn. Welll.... wouldn't it seem like a good candidate for adding more flexibility for plugins? What I mean is, it's a narrow use case, and I think we had talked about narrow use cases as candidates for user-defined plugins. OR optional core plugins (which isn't defined yet).
So, to me, if this went in, it's either expanding core in one way or another. One would be specific to this use case, and one would be expanding plugins to support scenarios like this use case.
Yes, but in your example, it seems you're only doing a partial solution with the plugin, and then doing the other part in a recursive mixin. My question was basically if a plugin could do this (from one of my examples above:
it seems you're only doing a partial solution with the plugin
I could just as well put the loop construct into the actual function. I chose not to, to illustrate that you can infact have full lambda delegate functionaity just by binding variables into the detached ruleset scope.
Infact; check the same gist again. I just updated it with named variables support...
Based on your gist, it looks like you're agreeing with @seven-phases-max. No?
The argument was that you could neither pass identifiers like a mixin to a function (so you could pass parameters to the mixin serving as a lambda delegate) nor execute a function without assigning its output to a variable or property (preventing you from using it to make a lambda that can emit CSS).
In my case, I use the function to wrap a second detached ruleset around the original, which injects the lambda's 'parameters' (very similar to how Definition.prototype.evalCall works when passing parameters into mixins, actually) and then I return that wrapped ruleset as a result.
A detached ruleset, ofcourse can be called to emit CSS.
What you show there is a mixin that is not different from for example this (there you just simplify Less loop via some lambda function) but to be fair the plugin is sort of redundant there as we can write a mixin with the identical functionality without any plugin (if I'm not mistaken the plugin there is used only to ban outer scope variables).
So, no, to cancel:
So, no, for now it's simply impossible to implement such each via plugin.
You need to show each as a function not as a mixin ;) (otherwise it's just the same discussion as after #1943 (comment)).
The argument was that you could neither pass identifiers like a mixin to a function
And you still can't. DR is not a mixin! :) (it can't have parameters for example). E.g. as a user I want my code to have lambda args named @v and @i (or whatever I need) I don't want that predefined @item and @index bloatware! ;)
as a user I want my code to have lambda args named @v and @i (or whatever I need) I don't want that predefined @item and @index bloatware! ;)
Did you miss the part where the parameter names are custom bindable? My example has them fixed, but you could have them passed in as an additional argument to whatever mixin is going to process the lambda ruleset. Using my gist as an example:
.each(@list, @params, @ruleset) {
@length : length(@list);
.iterate(1);
.iterate(@index) when (@index=<length) {
@item : extract(@list, @index);
@out : lambda(@item, @index, @params, @ruleset);
@out();
.iterate(@index+1);
}
})
.each(foo bar baz, v i, {
value-@{i} : @v;
});
That's not very DRY though and it puts burden on mixins offering callback semantics to set this kind of parameter configuration up. A better way would be to split the lambda function itself out into two components:
a bind(@name-a, @name-b, ..., @ruleset), and
a pass(@bound-ruleset, @value-a, @value-b, ... )
At that point a caller has complete control over parameter names:
.each(foo bar baz, bind(v, i, {
value-@{i} : @v;
});
and internally the mixin would not have to worry about them, and simply call pass and then evaluate the resulting wrapped detached ruleset.
Sure; it's still not a real native lambda, but it gets awfully close, doesn't it? ;-)
Ofcourse, it's all still a hack that would hopefully be replaced with native language support at some point in time. But until then it's a nice semantically (and almost syntactically) compatible 'polyfill'.
Actually, we may want to do do @key AND @index (and @value), in case someone wants to do something with the position of a rule in a map.
This makes me wonder what @index means in the context of a map. That is, in a list loop, you could call extract() and pass the index. It seems like there are a lot of caveats and inconsistencies introduced this way (e.g. extract(@looped-thing, @index) works in some loops, but not all).
@map: {
foo: bar;
baz: qux;
};
each(@map, {
-less-log: extract(@map, @index); // → ???// Or would this mean anything? (Certainly after a unification of access and extract that I've seen floated.)-less-log: @map[@index]; // → ???
})
So, in the implementation of this, I ran into an issue of nested each(), and wondering if we need named params. In an attempt to name params using (@value, @key) { }, I realized that was waaaay ambiguous for parsing, if we wanted to later do "anonymous mixins" or "detached rulesets with args". As soon as you write @dr: (@value ... up until then, this could finish with @dr: (@value + 1); or @dr: (@value, @key) { }, so it forces backtracking.
That is to say, two points:
Named vars are required if you ever want to nest each() functions. As a first implementation, they're probably not needed? Maybe not address nesting at first? But if you want them later.... how are we going to reconcile injected vs. declared?
I just wanted to make the point that this form for anonymous mixins/dr-args @dr: (@value, @key) { } is really too ambiguous.
So, the thing is, if you ever WANT nesting, then maybe injecting vars is the wrong approach. And if you need named vars, then just a plain (@value, @key) { } for DR args is the wrong approach IMO for Less.
Or maybe this is the wrong approach to support each(). I dunno...
I was thinking about some specifier to make it clear you're defining a mixin. I'd think . should also be acceptable, since standard "nonymous" mixins allow either . or #.
In other words, conceptually, we're just expanding the current mixin form to include no char after the . or #, though they wouldn't behave the way standard mixins would, in that they wouldn't be "author-callable", and they wouldn't allow "overloading" ('cause they'd be defining unique one-off mixins instead of additions to existing ones.
This makes me wonder what @index means in the context of a map. That is, in a list loop, you could call extract() and pass the index. It seems like there are a lot of caveats and inconsistencies produced this way.
I don't see any inconsistencies produced there. You can change your example to something that works with the current PR.
In other words, conceptually, we're just expanding the current mixin form to include no char after the . or #, though they wouldn't behave the way standard mixins would, in that they wouldn't be "author-callable".
Probably could be author-callable eventually, but not-callable could work for this first use-case? I wondered which to go with: #( or .(, but I like the idea of either/or.
Probably could be author-callable eventually, but not-callable could work for this first use-case?
I'd think "anonymous" would suggest "not author-callable", but maybe "anonymous" isn't the word. Maybe this is something different.
Also (and I edited the other comment with this thought, too, but I'll put it here so it isn't as hidden), I think the major difference is that .(){} and #(){} wouldn't be overloadable/stackable.
And that would mean authors would have no specific way to reference and call a specified .(){} mixin, which means "anonymous" is what they are and we can just call them that straight-up.
Seems like a fairly useful pattern. You could also pass around mixin definitions to other mixins, which you can't do now. (In 3.5 you can pass in mixin calls i.e. the resulting ruleset to a mixin, but not definitions.) That's where I was going with it; that is, the reason why a prefix is needed.
If detached rulesets are useful to pass around and call, then it makes sense that rulesets w/ args would be just as useful/powerful, if not more so.
That's why I would want this pattern, if used for each(), to be something that could be generalized at some point.
I'd think "anonymous" would suggest "not author-callable"
The "anonymous" in "anonymous function" comes from the fact that it is constructed without its own named reference.
Without its own named reference, it cannot be directly accessed through name by code at other locations. In that sense, such a function is "not directly author-callable," but is still callable if the author assigns the original function reference to a variable.
So yes; "anonymous mixin" is definitely the correct terminology here.
I think the added # or . makes a lot of sense and not just from a parser disambiguation point of view.
.( a ) { } is to .foo( a) { } in Less as function( a ) { } is to function foo( a ) { } in JavaScript.
It literally reads as: "here I declare a mixin" ( the leading . ) "which I won't give a name for further reference."
The only alternative syntax that would make sense is imho to clone the arrow-function syntax from JS, C#, etc. and make it like:
each(@list; (@value, @key, @index) => { })
That would work as well, but is probably much harder to implement as it requires a lot of look-ahead through the arguments list to find the => marker, whereas if you find the .( or #( sequence at the head you already know you'd be dealing with an anonymous mixin without much - if any - lookahead required.
That would work as well, but is probably much harder to implement as it requires a lot of look-ahead through the arguments list to find the => marker, whereas if you find the .( or #( sequence at the head you already know you'd be dealing with an anonymous mixin without much - if any - lookahead required.
I mean this is my main reason; #() / .() is just easier to implement, but I wouldn't push it if the syntax didn't gel with people. If it gels, then I think it's working as expected in #3263. Please review when you get the chance!
Activity
seven-phases-max commentedon Nov 4, 2014
Just in case this is not ugly hack but well defined language facility.
(I also can't get the idea of solving "up-chain scope issues" with "down-chain parameter pass feature", these do not seem to be related, do they? But well, never mind, the feature remains useful regardless of this anyway)
Well, either way the idea itself is quite self-revealing and the only problem is ambiguous syntax, notice:
In other languages with such facility this problem is solved with some specific keyword/operators preceding such lambda (e.g.
[]in C++ or justfunctionin JavaScript).rjgotten commentedon Nov 4, 2014
When I mention it being a 'hack', I specifically mean that it seems hacked into the existing language desgin (to facilitate return values as a later addition), rather than something that was thought about up front. I don't mean the actual code is of low quality. Actually; the LESS parser and AST are quite well done. (Tip of the hat for that.)
However, the fact that it's well-defined and well-engineered doesn't make it less ugly when used. It's a language feature that is completely counter to intuition for most, as no other mainstream language does this.
May I suggest
@a: (@b) => { b : b }?It's reminiscent of both C#'s lambda delegates and JavaScript's ES6 Harmony arrow notation for functions with preserved lexical scope. Provided you place detached rulesets at the head of the types the parser tries to apply for expressions (which I think it already is), this should result in a deterministic parsing without ambiguities.
And if you really wanted to have a list with an explicit
=>symbol in there, well; there's always~"=>"as an escape hatch. (It's not like you don't already have this problem withcalc()to some degree.)seven-phases-max commentedon Nov 4, 2014
(Though we're quickly going offtopic - Yes, it was not something thought of the earlier Less days but today it still remains the only way to "return" something from something in Less (since the "function" facility is still somewhat neglected, though actually the whole thing is not limited to returning variables). So I insist :) that "parent scope leak" has nothing to do with lambdas, after all lambdas do not return anything at all. And the rest is another unrelated story).
@a: (@b) => { b : b }- Yes, could be something like this (though personally I extremely don't like=>:)matthew-dean commentedon Feb 5, 2015
Per the discussion in #1943, and also related to passing mixins by reference (issue # ?), I would suggest this syntax:
...equal to...
...equal to...
However, there's no reason that you shouldn't be able to use the same construct for user-defined mixins:
.for(@list, .mixin);and do whatever you want with the list / mixin parameters.To be accurate, this is an extension of mixins / parameters, and not detached rulesets, and passing mixins by reference has been discussed elsewhere (although I couldn't find it with a brief search).
seven-phases-max commentedon Feb 5, 2015
It was mentioned in #965 (comment) and then in more details discussed #1648 (comment) and following comments.
matthew-dean commentedon Mar 17, 2015
Per separate discussion with
@plugin, this now seems like an ideal candidate to close in favor of a user-defined function via@plugin.matthew-dean commentedon Mar 17, 2015
Also, this is strangely relevant to #1505 which I just commented on, because we're demonstrating passing a CSS list to a custom function.
seven-phases-max commentedon Mar 17, 2015
Note that currently you can neither pass identifiers like
.mixinto a function (not allowed by the parser) nor you can call a function w/o assigning its result to something... So, no, for now it's simply impossible to implement sucheachvia plugin.matthew-dean commentedon Mar 17, 2015
Oh. Damn. Welll.... wouldn't it seem like a good candidate for adding more flexibility for plugins? What I mean is, it's a narrow use case, and I think we had talked about narrow use cases as candidates for user-defined plugins. OR optional core plugins (which isn't defined yet).
So, to me, if this went in, it's either expanding core in one way or another. One would be specific to this use case, and one would be expanding plugins to support scenarios like this use case.
rjgotten commentedon Mar 24, 2015
Oh... I wouldn't be too sure about that.
matthew-dean commentedon Mar 24, 2015
Yes, but in your example, it seems you're only doing a partial solution with the plugin, and then doing the other part in a recursive mixin. My question was basically if a plugin could do this (from one of my examples above:
Based on your gist, it looks like you're agreeing with @seven-phases-max. No?
rjgotten commentedon Mar 24, 2015
I could just as well put the loop construct into the actual function. I chose not to, to illustrate that you can infact have full lambda delegate functionaity just by binding variables into the detached ruleset scope.
Infact; check the same gist again. I just updated it with named variables support...
The argument was that you could neither pass identifiers like a mixin to a function (so you could pass parameters to the mixin serving as a lambda delegate) nor execute a function without assigning its output to a variable or property (preventing you from using it to make a lambda that can emit CSS).
In my case, I use the function to wrap a second detached ruleset around the original, which injects the lambda's 'parameters' (very similar to how
Definition.prototype.evalCallworks when passing parameters into mixins, actually) and then I return that wrapped ruleset as a result.A detached ruleset, ofcourse can be called to emit CSS.
This wrapping layer solves both problems.
seven-phases-max commentedon Mar 24, 2015
@rjgotten
What you show there is a mixin that is not different from for example this (there you just simplify Less loop via some
lambdafunction) but to be fair the plugin is sort of redundant there as we can write a mixin with the identical functionality without any plugin (if I'm not mistaken the plugin there is used only to ban outer scope variables).So, no, to cancel:
You need to show
eachas a function not as a mixin ;) (otherwise it's just the same discussion as after #1943 (comment)).And you still can't. DR is not a mixin! :) (it can't have parameters for example). E.g. as a user I want my code to have lambda args named
@vand@i(or whatever I need) I don't want that predefined@itemand@indexbloatware! ;)rjgotten commentedon Mar 24, 2015
Did you miss the part where the parameter names are custom bindable? My example has them fixed, but you could have them passed in as an additional argument to whatever mixin is going to process the lambda ruleset. Using my gist as an example:
That's not very DRY though and it puts burden on mixins offering callback semantics to set this kind of parameter configuration up. A better way would be to split the lambda function itself out into two components:
bind(@name-a, @name-b, ..., @ruleset), andpass(@bound-ruleset, @value-a, @value-b, ... )At that point a caller has complete control over parameter names:
and internally the mixin would not have to worry about them, and simply call
passand then evaluate the resulting wrapped detached ruleset.Sure; it's still not a real native lambda, but it gets awfully close, doesn't it? ;-)
Ofcourse, it's all still a hack that would hopefully be replaced with native language support at some point in time. But until then it's a nice semantically (and almost syntactically) compatible 'polyfill'.
100 remaining items
calvinjuarez commentedon Jul 8, 2018
This makes me wonder what
@indexmeans in the context of a map. That is, in a list loop, you could callextract()and pass the index. It seems like there are a lot of caveats and inconsistencies introduced this way (e.g.extract(@looped-thing, @index)works in some loops, but not all).matthew-dean commentedon Jul 8, 2018
So, in the implementation of this, I ran into an issue of nested
each(), and wondering if we need named params. In an attempt to name params using(@value, @key) { }, I realized that was waaaay ambiguous for parsing, if we wanted to later do "anonymous mixins" or "detached rulesets with args". As soon as you write@dr: (@value... up until then, this could finish with@dr: (@value + 1);or@dr: (@value, @key) { }, so it forces backtracking.That is to say, two points:
each()functions. As a first implementation, they're probably not needed? Maybe not address nesting at first? But if you want them later.... how are we going to reconcile injected vs. declared?@dr: (@value, @key) { }is really too ambiguous.Maybe something more like this in the future:
So, the thing is, if you ever WANT nesting, then maybe injecting vars is the wrong approach. And if you need named vars, then just a plain
(@value, @key) { }for DR args is the wrong approach IMO for Less.Or maybe this is the wrong approach to support
each(). I dunno...calvinjuarez commentedon Jul 8, 2018
I was thinking about some specifier to make it clear you're defining a mixin. I'd think
.should also be acceptable, since standard "nonymous" mixins allow either.or#.In other words, conceptually, we're just expanding the current mixin form to include no char after the
.or#, though they wouldn't behave the way standard mixins would, in that they wouldn't be "author-callable", and they wouldn't allow "overloading" ('cause they'd be defining unique one-off mixins instead of additions to existing ones.matthew-dean commentedon Jul 8, 2018
@calvinjuarez
I don't see any inconsistencies produced there. You can change your example to something that works with the current PR.
outputs:
You can also do:
Which outputs:
Or are you simply pointing out that you can't actually use
@indextoextract()on a map? Because I think that's a completely separate issue.matthew-dean commentedon Jul 8, 2018
Probably could be author-callable eventually, but not-callable could work for this first use-case? I wondered which to go with:
#(or.(, but I like the idea of either/or.calvinjuarez commentedon Jul 8, 2018
$@varworks!? I had no idea! I thought it was nixed! That makes me so happy!matthew-dean commentedon Jul 8, 2018
Ha, yeah I slipped it in bug fixes before I released 3.5. ^_^
calvinjuarez commentedon Jul 8, 2018
I'd think "anonymous" would suggest "not author-callable", but maybe "anonymous" isn't the word. Maybe this is something different.
Also (and I edited the other comment with this thought, too, but I'll put it here so it isn't as hidden), I think the major difference is that
.(){}and#(){}wouldn't be overloadable/stackable.And that would mean authors would have no specific way to reference and call a specified
.(){}mixin, which means "anonymous" is what they are and we can just call them that straight-up.matthew-dean commentedon Jul 8, 2018
@calvinjuarez
Wouldn't we eventually do this?
The reason being what you said: i.e. vars are not overloadable. So you could essentially "replace" mixins by reassigning the variable.
Seems like a fairly useful pattern. You could also pass around mixin definitions to other mixins, which you can't do now. (In 3.5 you can pass in mixin calls i.e. the resulting ruleset to a mixin, but not definitions.) That's where I was going with it; that is, the reason why a prefix is needed.
If detached rulesets are useful to pass around and call, then it makes sense that rulesets w/ args would be just as useful/powerful, if not more so.
That's why I would want this pattern, if used for
each(), to be something that could be generalized at some point.rjgotten commentedon Jul 9, 2018
The "anonymous" in "anonymous function" comes from the fact that it is constructed without its own named reference.
Without its own named reference, it cannot be directly accessed through name by code at other locations. In that sense, such a function is "not directly author-callable," but is still callable if the author assigns the original function reference to a variable.
So yes; "anonymous mixin" is definitely the correct terminology here.
matthew-dean commentedon Jul 9, 2018
@rjgotten Thanks, that's a great explanation.
What do you think about the syntax tweak / PR?
rjgotten commentedon Jul 9, 2018
I think the added
#or.makes a lot of sense and not just from a parser disambiguation point of view..( a ) { }is to.foo( a) { }in Less asfunction( a ) { }is tofunction foo( a ) { }in JavaScript.It literally reads as: "here I declare a mixin" ( the leading
.) "which I won't give a name for further reference."The only alternative syntax that would make sense is imho to clone the arrow-function syntax from JS, C#, etc. and make it like:
That would work as well, but is probably much harder to implement as it requires a lot of look-ahead through the arguments list to find the
=>marker, whereas if you find the.(or#(sequence at the head you already know you'd be dealing with an anonymous mixin without much - if any - lookahead required.matthew-dean commentedon Jul 9, 2018
I mean this is my main reason;
#()/.()is just easier to implement, but I wouldn't push it if the syntax didn't gel with people. If it gels, then I think it's working as expected in #3263. Please review when you get the chance!Merge pull request #3263 from matthew-dean/feature/each-2