Fallthrough sections #178
Replies: 2 comments 11 replies
-
yes, switch (value) {
case 1:
// ...
// fallthrough to 2
case 2:
// ...
break;// no fallthrough
default:
// ...
break;
}an absense of the next Q to the public and implementers is whether stack pushing needed: with data [
"animal" => "mouse"
]in my implementation currently becomes: do you agree with that behavior/logic? EDIT: oke, i forgot that fallthrough is actually utilized by using this extended syntax: +EDIT switch (number) {
case 1:
case 2:
case 3:
case 4.5:
// ...
break;
case 1:
// ...
break;
default:
// ... non zero
break;
inverted_default:
// ... zero
break;
} |
Beta Was this translation helpful? Give feedback.
-
any example that has more that two render variants of the same entity. for example a it can be:
so the analyzer creates github discussion is putting quotation at the top, which is wrong, but i wont fix it |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
@determin1st has been championing a variant of this idea in several other discussions. For reference, I will link two posts where he described it: #157 (comment) and before that, #135 (comment). The present discussion is dedicated to any feature that would give Mustache a concise notation for if/elif/elif.../else like fallthrough cases.
The long time status quo in Mustache is that you need to repeat a condition in order to do an if/else:
If you lean very much to the pragmatist side (#163), situations like the above make it tempting to introduce an
elsekeyword into the language:I feel this would be very un-Mustachy.
{{else}}should just be an interpolations tag that looks for anelsekey in the context.It gets a lot better already if you use a
|sigil instead of anelsekeyword:In combination with an inverted section, this notation also lets you do something that currently cannot be done without data preprocessing (see option 9 in #157):
It's just that the use case is a bit narrow. However, if
|is a sigil, that means there could potentially be some kind of payload after it. That would make it possible to extend this syntax to a Mustache equivalent of a switch statement with multiple fallthrough cases (note that this relies on #175 to avoid a confusing closing tag):We save ourselves three tags per case in this way. If we compare this to status quo Mustache, the saved effort is quite substantial even for just three cases:
I suspect this is the idea that @determin1st originally came up with and that eventually evolved into the feature he implemented in sm-mustache (see references above). The idea that he actually implemented is more intricate than what I illustrated here; it includes equality comparisons to literal values and multiple fallthrough cases within a single
|tag. I personally think those intricacies deviate too much from Mustache's core principles, so I defer to @determin1st if he feels like discussing them.Continuing from the above example, I see two problems. Firstly, it makes sense that
{{|cases can be chained both after{{#and after{{^. It also makes sense that conditions in a{{|can be written in the same way as in a{{#or a{{^, so with dotted names that resolve to the context. However, this means that it is ambiguous whether the{{|itself is a positive (#) or a negative (^) condition. It is easy to imagine situations where one might want to chain a mixture of positive and negative cases, so it would be too restrictive to always use the sign of the{{#or{{^that opened the chain.Secondly, the
|could be confused for the|operator in filter syntax (#153), an idea I'm more fond of than fallthrough sections.The first solution that I thought of, but which does not work, is to simply start a new
{{#or{{^without closing the previous one. This does not work, because it is impossible for a parser to distinguish from nested sections with omitted closing tags. It also means that the|sigil is still required, just to mark the default case:Another option, which would work but which I dislike, would be to use double sigils:
Or yet another option that I would dislike: add a second sigil specifically for negative fallthrough cases, for example
{{\.I don't see an acceptable way yet in which fallthrough cases could be implemented. Despite that, the idea has grown on me, so I hope for another round of inspiration!
Beta Was this translation helpful? Give feedback.
All reactions