question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow parametrized mixins as detached rulesets to form 'lambdas'

See original GitHub issue

It seems that currently LESS only supports ‘pure’ rulesets to be passed along as mixin arguments or stored in variables as detached rulesets.

I suggest to extend this support to incorporate parametrized mixins, essentially giving LESS the capability to work with lambdas.

E.g. One would be able to write

.list {
  .forEach(foo bar baz, (@item, @index) {
    @i : (@index + 1);
    > li:nth-child(@{i}):before {
      content : "@{item}";
    }
  });
}

where .forEach is defined as

.forEach(@list, @lambda) {
  @n : length(@list);

  .for(0)
  .for(@index) {}
  .for(@index) when (@index < @n) {
    @lambda(extract(@list, @index), @index);
    .for(@index + 1);
  }
}

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.)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:118 (111 by maintainers)

github_iconTop GitHub Comments

3reactions
matthew-deancommented, Jul 8, 2018

$@ works!? I had no idea! I thought it was nixed! That makes me so happy!

Ha, yeah I slipped it in bug fixes before I released 3.5. ^_^

1reaction
rjgottencommented, Jul 9, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Passing rulesets to mixins | Less Web Development Essentials
Detached rules can be called by appending parentheses after their names. The content of the detached ruleset has been copied to the position...
Read more >
Detached rulesets in LESS as a lambda-expressions
In essence it means an ability to store a block of LESS code into variable or pass it as an argument to mixin....
Read more >
Send properties as argument for mixin - css - Stack Overflow
Starting with Less v1.7.0, it is possible to pass detached rulesets as parameter to a mixin. Quoting Less Website: Detached ruleset is a ......
Read more >
Crash Course: entity-component system - EnTT
The function type for free functions and decayed lambdas passed as parameters to emplace is void(const void *, entt::registry &) instead. The first...
Read more >
Semantic of Function Parameters and Return Values
Parameter passing semantic rules: I can make this subsection quite short. Most of the rules are already explained in the post to the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found