[
https://issues.apache.org/jira/browse/FREEMARKER-73?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16185737#comment-16185737
]
Daniel Dekany edited comment on FREEMARKER-73 at 9/29/17 12:10 PM:
-------------------------------------------------------------------
{{nullOrMissing?sequence}} is an error, like with almost all built-ins (the
exception is {{?has_content}} and the like). Otherwise
{{theresATypolnThis?sequnce}} would silently suppress the typo. If you know
that the left hand operand can be {{null}}/missing, then you should write
{{somethimesNull!?sequnce}}. That's what is consistent with the behavior
elsewhere too (and at least in FM2 that shouldn't be changed).
The case with scalars (including string) is somewhat similar. You have an
improper type there, which probably means that you have made a mistake, hence,
you better off with an error. Other built-ins and operators do that too. If a
variable is a scalar, then it's likely that it will never be a list-like value,
and there's just a mistake there. Unless there's some very frequent use case
for this, I would keep such a conversion explicit.
This no-magic, fail-fast approach is characteristic of FreeMarker.
BTW, any kind of type related magic can easily lead to confusing edge cases in
FreeMarker, because values can have multiple types. Like DOM nodes are
routinely both sequences and strings on the same time.
was (Author: ddekany):
{{nullOrMissing?sequence}} is an error, like with almost all built-ins (the
exception is {{?has_content}} and the like). Otherwise
{{theresATypolnThis?sequnce}} would silently suppress the typo. If you know
that the left hand operand can be {{null}}/missing, then you should write
{{somethimesNull!?sequnce}}. That's what is consistent with the behavior
elsewhere too (and at least in FM2 that shouldn't be changed).
The case with scalars (including string) is somewhat similar. You have an
improper type there, which probably means that you have made a mistake, hence,
you better off with an error. Other built-ins and operators do that too. If a
variable is an {{""}} or {{false}}, then it's likely that it will never be a
list-like value, and there's just a mistake there. Unless there's some very
frequent use case for this, I would keep such a conversion explicit.
This no-magic, fail-fast approach is characteristic of FreeMarker.
BTW, any kind of type related magic can easily lead to confusing edge cases in
FreeMarker, because values can have multiple types. Like DOM nodes are
routinely both sequences and strings on the same time.
> ?seq to convert some models to a sequence
> -----------------------------------------
>
> Key: FREEMARKER-73
> URL: https://issues.apache.org/jira/browse/FREEMARKER-73
> Project: Apache Freemarker
> Issue Type: Bug
> Components: engine
> Affects Versions: 2.3.26-incubating
> Reporter: Ondra Žižka
> Fix For: 2.3.27-incubating
>
>
> The objects I feed to FreeMarker often cotain {{Iterator}}s rather than
> {{Collection}}s.
> While it's quite easy to write a FreeMarker function
> {{iteratorToList(Iterator)}}, it would be convenient to have the following:
> A built-in that would convert various models to a sequence. Could be named
> {{?seq}} or {{?sequence}}.
> Usage:
> {code}<#assign foo = myIterable?seq>{code}
> {code}<#assign foo = myIterator?seq>{code}
> * A sequence would stay intact.
> * An {{Iterable}} wrapped in FM model would become internally a {{List}} -
> all elements consumed and stored.
> For me, other cases are not that important, but with some imagination, other
> models could also leverage it, like,
> * A {{Stream}} could be also turned into a {{List}}.
> * A hash could turn into a sequence of key/value pairs. That could be useful
> for debugging purposes.
> I am not sure if I didn't propose that already, but I could only find
> FREEMARKER-62.
> Thanks for considering.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)