Lambda arguments #188
Replies: 4 comments 2 replies
-
oke, ill try comment on and describe the lambda interface and the argument, how it's envisioned by
yes, the whitespace should be the trigger for the (singular) argument:
should also work as lambda with function relativeDate(object $m, string $a): string
{
$v = $m->value($a);# here the value is obtained
return date("Y-m-d H:i:s", $v);
}
i came to function lambda(object $m, string $a); variant. the return type must be fixed, one of function text(): string;# returns primary section's content
function value(string $path, mixed $val=null): mixed;# gets or sets a value on the context stack
# etc which cover most of the needs
a wrap implementation could be function wrap(object $m, string $a): string
{
$delim = explode(",", $a);
return $delim[0].$m->text().$delim[1];
} authors are free to choice separators/behavior and how it composes into result - not a mustache's headache. |
Beta Was this translation helpful? Give feedback.
-
@determin1st in #187 (reply in thread):
I will give you two advantages. Firstly, lambdas-with-arguments burden the lambda author with the responsibility to perform context lookups. The lambda implementation requires knowledge about the API of the Mustache engine. Filters do not; they can just be straightforward data conversion functions. Contrast these two notations and their corresponding implementations:
function relativeDate(object $m, string $a): string
{
$v = $m->value($a);# here the value is obtained
return date("Y-m-d H:i:s", $v);
}
function relativeDate($v): string
{
return date("Y-m-d H:i:s", $v);
} Secondly, filters can be easily composed while lambdas-with-arguments cannot. Suppose I sometimes want to start the relative date with an uppercase. With filters, I can just write this to get
Achieving the same effect with lambda arguments is either going to involve inconsistency, where you sometimes use the lambda with arguments and sometimes with section content,
or some kind of combinatorial explosion, where lambdas need to be aware of each other's implementations.
|
Beta Was this translation helpful? Give feedback.
-
that was your idea/example to wrap (emphasize) something with filters. i only showed how it's done with lambda argument, the wrap is more generic and may contain some advanced logic to be even more useful. to improve it a bit, it can become
you have to understand that there is no magic in the world, energy goes from one place to another, same is the burden, it doesn't disappear. it is a correct placement of the burden - into the code, out of template, according to
if you wish to write lambda, you need to know mustache api, if you only want to use lambda, you have to know what it does, how it is applied, a short description. same applies to filters.
well, not much of a contrast. westerners feel more comfortable working with LEFT-TO-RIGHT notations than arabic (or kind of.. are we in terminal?) RIGHT-TO-LEFT notations. but more importantly, the auto-resolution of the path closes the possibility of working with literals. reducing implementation by one line isnt a real profit.
well yeah, if you want to combine functions, you have to write lambda that combines them, although im not a fan of making such things in the template, it will look like
if youre up to marry a |
Beta Was this translation helpful? Give feedback.
-
Yes it does, in this version:
no literal is possible. to open the road to literals, the burden must grow up a bit, it costs one line of code for the lambda author. though, mustache implementer will have to sweat - the cost of a feature.
its the road to inconsistency. a good lambda must bear a bunch of checks and balances. for example, an unpredictable result will arise from a free form of combinations - a function that expects a string input but gets another type, like
inconsistency meaning: the fact that someone or something cannot be trusted to stay the same in behaviour or quality in all situations - imo, a function (is it a function here or a boolean flag?) that applies differently conforms to that definition.
im not a fan of combinatorics in the template, that brings more logic into template and makes author treat template language as a programming language. lambda argument empowers lambda implementer the first place. composing a set of unbound functions in the template wont add to readability or ease of use, but i agree that "filters" are easier to implement. it is also an imported idea (lets do like the other engines did, right-to-left, console terminals), foreign to mustache principle |
Beta Was this translation helpful? Give feedback.
-
This idea is not my own. In fact, I am not in favor of it. Still, I believe it deserves a dedicated discussion, because @determin1st is currently promoting it.
Several implementations extend the Mustache language by allowing template arguments, for example like this:
If
article.published
is a value in the context with some language-specific representation of January 13 2024 andrelativeDate
is a lambda, then the above might outputyesterday
in implementations that support this notation.Depending on the implementation, there might be one or more arguments. Arguments might be context references, literal values or a bit of both. In #135 (comment), @determin1st suggested allowing any arbitrary string as payload, leaving its interpretation to the lambda in question:
He provided a rationale in #187 (comment):
In order for the above examples to work with @determin1st's proposed API, Mustache would also need to give lambdas access to the context stack. In other words, it depends on (some of) features 1-3 of power lambdas (#155). I have dubbed "introspective lambdas" as a name for such lambdas in #155 (reply in thread). The main alternative for lambda arguments, as well as for introspective lambdas, is filters (#153).
Beta Was this translation helpful? Give feedback.
All reactions