Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: lambdas (value-producing "variable" functions) #1368

Closed
extemporalgenome opened this issue Jun 13, 2013 · 1 comment
Closed

Proposal: lambdas (value-producing "variable" functions) #1368

extemporalgenome opened this issue Jun 13, 2013 · 1 comment

Comments

@extemporalgenome
Copy link
Contributor

Currently LESS has good support for mixins that inject values and variables and mathematical expressions, but seemingly not value-producing functions that can be used in expressions similarly to how the builtin functions can be used. Since use of embedded JavaScript is considered an implementation detail, this could be used to portably handle many code deduplication needs, especially when the ability to specify functions to the LESS interpreter is tedious or impossible.

Proposed syntax (with trivial examples):

@div(@x, @y): (@x / @y);
@five: @div(10, 2);
@eight: @div(16, @div(6, 3));

The function body would be limited to valid LESS expressions including interpolation. Within a given scope, it would be reasonable that a name can either be defined as a variable or a @function, but not both. Although the above would be very useful on its own, it would additionally be useful to be able to utilize guard expressions, such as:

@div(@x, @y): (@x / @y);
@div(@x, 0): 0; // fallback value for errors

@min(@x, @y): @x;
@min(@x, @y) when (@y < @x): @y;
@max(@x, @y): @min(@y, @x);

Though the particular semantics and syntax are not important, in this subproposal, multiple definitions of the same name in the same scope augment the same @function, with precedence in order of: 1) arity, 2) pattern matching, 3) guard expressions. Rather than "all that apply", the most specific that applies would be chosen, and it should at least be a runtime error if multiple definitions at the same specificity apply. Definitions would never augment @functions with the same name from outer scopes, but would rather define a new @function that shadows the old one (just as variables do).

@lukeapage
Copy link
Member

See #538

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants