Post by Daniel DekanyPost by Jonathan RevuskyPost by Daniel DekanyPost by Jonathan RevuskyAFAICS, what it does now is about right. If you pass in something that
is deliberately null, that is okay, but if it is a mispelled variable
name, you get an exception.
That was clear; you doesn't seem to really address what I said. OK,
for starters, consider this: is it a good idea that the default value
of macro parameters is not used if the value received for the
parameter is null (not undefined)?
I'm not 100% sure. I have been thinking about it.
Well, I grant, you're probably right about the above point -- as usual.
However, I see that as a separate issue from what happens if you pass in
null or undefined variable when there is no default value defined for
that argument.
When there is no default value, it seems to me to be very logical to say
that the null deliberately defined as null (i.e. coming from a java API
or just a [#set foo = null] should be passed in
The only problem is that usually null-s are not coming from such
sources, I mean, from source where you have explicitly specified null
*in the template*. They are coming from bean properties and return
values of Java methods.
If they come from the return value of a java method or a bean property
(which is usually just getFoo() return value) then it is a java null,
just as if it came from [#set foo = null]
If it comes from a hash where the key/value pair was not put in
explicitly, then it is a missing value.
Post by Daniel DekanyPost by Jonathan Revuskyand the undefined value, which is likely the result of a misspelled
variable name, should cause an exception.
As for what happens when you have a default value, it seems that,
probably, you should go to the default value if it's a true deliberate
null, but still blow up when it's truly undefined. Otherwise, what are
you saying? That we have this default value here just in case you
misspelt a variable, so that we can keep going? That obviously isn't the
purpose of a default value, is it?
The treatment of the parameter expressions in the macro call and how
the macro reacts to the actual parameter values are separate issues.
For example *as far as the macro call statement is concerned*, this
[#set x = null]
BUT the foo macro may will say that, "No thank you, I don't want a
null here. I wanted a title, and you have provided nothing. Because
null is just nothing as far as I'm concerned.".
Yes, but that error comes later, when the macro tries to dereference x
and expects a string, but gets a null. But no error occurs at the
point where the macro is invoked. Well, it does occur when the macro
is invoked if x is just undefined, but in the example above, x is not
undefined, it is defined, but null, the distinction we didn't have
before.
(And with the same
Post by Daniel Dekanylogic a default parameter value step in action of the parameter value
is null.)
Okay, I am willing to agree with you on that point, assuming it is a
true java null, and not simply an undefined variable. If it's just an
undefined variable, AFAICS, the code might as well blow up at the
first opportunity.
Post by Daniel DekanyReally, poor "foo" macro why should tolerate that null when it was not
designed to handle it? I think most macros are not designed to. Those
which are could explicitly declared as "I can handle a null as the
value this parameter". Something like {bypass_null} as I have shown.
(It's "bypass" and not "allow" because the null will avoid the default
value of the parameter, if there is one.)
I don't see enough value added in introduing some extra bypass_null
keyword or something. We should just decide that JAVA_NULL gets turned
into the default parameter if a default parameter is present and
document that. And, as far as I'm concerned, we can just maintain a
policy of zero tolerance towards undefined variables.
Post by Daniel DekanyPost by Jonathan RevuskyPost by Daniel DekanyI mean, surely you can reason for
it on theoretical grounds (i.e. that the parameter was specified in
the caller site), but does it make sense practically? Like if you have
foo.bar used as macro parameter that has a default value, and foo.bar
is an existing bean property whose value is null (which is the way in
Java to indicate "missing"), then didn't you meant to use the default
value of the parameter? And if in the same situation foo is a Map,
that doesn't contain the key "bar"?
Well, I think the approach is that, if you want the subvariable to
be a genuine null, you have to explicitly put it in the map, and you
can use map.put("foo", null) on the java side to do that.
But almost nobody will, as we know. It's just not the way Map-s are
used in any language, and of course an MVC template language is not in
the position to dictate this.
Well, then we're just back to the same intolerant pre-2.4 behavior,
that we throw an exception because the expression is undefined. If you
want to be passing around nulls, you have to be explicit about your
intent. I don't see how we can do any better than this really.
Post by Daniel DekanyPost by Jonathan Revusky(The default object wrapper implementations will massage that to a
TemplateModel.JAVA_NULL in the wrapped FTL hash object...)
(Note that the template author
Post by Daniel Dekanydoesn't see if which is the case -- bean or map.)
That's true enough, but it is up to the java programmer to define the
data model.
And mostly up to us, as far as it comes to wrapping. Things just
should work senseful out of the box. The default wrapper is the prime
example of how things should work. If what is the best practice.
The default wrapper will wrap a java Map such that if there is a
key/null pair, the null gets massaged into the FTL JAVA_NULL singleton
and you can pass that around and it will get turned back into a
regular null if you pass it back into a java API method. If the
specific variable or subvariable is referenced but is not in the data
model, we get an exception.
Post by Daniel DekanyPost by Jonathan RevuskyAnd to communicate what is in the data model to the person
working on the template layer. The problem was that, before this release
cycle, a java programmer had basically no way to distinguish between a
deliberate null and something being null just because it was missing
(i.e. probably a misspelt variable) but now the distinction is clear.
So, concretely what would you do if you are "the java programmer" to
save the template authors from confusion?
Well, personally, as the first-best solution, I would try to have a
data model which does not have any java nulls in it, like using the
empty string or empty sequence or hash and so on. When that's not
possible, because the java API you're exposing potentially has null
return values, I would make sure to document that the values are
potentially null, under certain circumstances. In those cases, the
nulls will be the FTL JAVA_NULL. But in that case, any other missing
value is just undefined and still blows up at the earliest
opportunity.
JR
Post by Daniel Dekany--
Best regards,
Daniel Dekany
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
FreeMarker-devel mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-devel