You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by John McNally <jm...@collab.net> on 2001/08/03 03:21:50 UTC

variables retaining value in #macro

if I have a macro

#macro (example $foo)
  #set ($tempString = $foo.Name)
  ...
#end

And I have a template  

...
#set ($tempString = $confidential.Info)
#example($customer)
...

Then $tempString inside the macro will retain the value it in the
calling template, if $customer.Name returns null.

Is this considered a useful feature under some conditions?  If not,
would it not be better if a variable inside macro did not get
initialized, it had no value?

john mcnally

Re: variables retaining value in #macro

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
John McNally wrote:
> 
> Sorry for reposting.  I posted and then noticed I had forgot to
> subscribe, so I subscribed.  I checked the archives for any responses,
> but did not even see my post.  Thought it got lost into the ether.

No worries.  Last week and week before were kinda hectic, so I wasn't
sure if this was a repost (I thought it was a repost...) because there
was no answer, or a new issue and I had dreamed about the subject :)  I
am sorry if my reponse came across brusque.

> 
> So the answer is a macro should always be written with the assumption
> that variables are preinitialized and could contain irrelevant data. 

This is a good point, and I haven't a clue what the cosmically-correct
answer is.  

On one hand, it's no different than in regular VTL if you referenced
things that are non-existant in the context, which is consistant with
the original idea of VMs, as a device to assist designers in making
repeatable / callable display code.

On the other hand, people clearly use them for more.  We have no devices
in the language/runtime to help us (like references in C++ where you
can't pass a null if you tried..).  And even then, it doesn't matter,
because you can always call methods on an input, and they could return a
null...

#macro( foo $a )

  #set($lala = $a.getThing() )

  ...

#end

We could add (finally) the 

#if( $foo == NULL)

idea to allow a VM writer to check inputs.

#macro( foo $a )
  #if ($a != NULL)
     ...
  #end
#end

I am sure that someone will suggest a new macro directive that does this
automatically

#macrochecked( foo $a )

or something, but since you always can access methods that return a
null, I don't see the point.

The #if( $a == NULL) seems to have more general utility.

> I
> think this is likely to lead to bugs, as writers of macros could
> overlook something and a user of a macro would not necessarily have
> knowledge of what variables are used within the VM.  But I suppose the
> user will most likely be able to read the VM and can just avoid using
> any variables that are #set in the VM.

That's right.  However, there is the idea of localscope, where VMs
basically work on 'local context' references for working in the VM, and
only affect the global context references if they are passed in as args.

At least, thats the intention.

geir

> "Geir Magnusson Jr." wrote:
> >
> > John McNally wrote:
> > >
> > > if I have a macro
> > >
> > > #macro (example $foo)
> > >   #set ($tempString = $foo.Name)
> > >   ...
> > > #end
> > >
> > > And I have a template
> > >
> > > ...
> > > #set ($tempString = $confidential.Info)
> > > #example($customer)
> > > ...
> > >
> > > Then $tempString inside the macro will retain the value it in the
> > > calling template, if $customer.Name returns null.
> > >
> > > Is this considered a useful feature under some conditions?  If not,
> > > would it not be better if a variable inside macro did not get
> > > initialized, it had no value?
> >
> > Deja vu?  Didn't you ask this last week or something?
> >
> > This is no different than regular #set() behavior...
> >
> > geir
> >
> > --
> > Geir Magnusson Jr.                           geirm@optonline.net
> > System and Software Consulting
> > Developing for the web?  See http://jakarta.apache.org/velocity/
> > Be well, do good work, and keep in touch.

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: variables retaining value in #macro

Posted by John McNally <jm...@collab.net>.
Sorry for reposting.  I posted and then noticed I had forgot to
subscribe, so I subscribed.  I checked the archives for any responses,
but did not even see my post.  Thought it got lost into the ether.

So the answer is a macro should always be written with the assumption
that variables are preinitialized and could contain irrelevant data.  I
think this is likely to lead to bugs, as writers of macros could
overlook something and a user of a macro would not necessarily have
knowledge of what variables are used within the VM.  But I suppose the
user will most likely be able to read the VM and can just avoid using
any variables that are #set in the VM.

john mcnally 

"Geir Magnusson Jr." wrote:
> 
> John McNally wrote:
> >
> > if I have a macro
> >
> > #macro (example $foo)
> >   #set ($tempString = $foo.Name)
> >   ...
> > #end
> >
> > And I have a template
> >
> > ...
> > #set ($tempString = $confidential.Info)
> > #example($customer)
> > ...
> >
> > Then $tempString inside the macro will retain the value it in the
> > calling template, if $customer.Name returns null.
> >
> > Is this considered a useful feature under some conditions?  If not,
> > would it not be better if a variable inside macro did not get
> > initialized, it had no value?
> 
> Deja vu?  Didn't you ask this last week or something?
> 
> This is no different than regular #set() behavior...
> 
> geir
> 
> --
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.

Re: variables retaining value in #macro

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
John McNally wrote:
> 
> if I have a macro
> 
> #macro (example $foo)
>   #set ($tempString = $foo.Name)
>   ...
> #end
> 
> And I have a template
> 
> ...
> #set ($tempString = $confidential.Info)
> #example($customer)
> ...
> 
> Then $tempString inside the macro will retain the value it in the
> calling template, if $customer.Name returns null.
> 
> Is this considered a useful feature under some conditions?  If not,
> would it not be better if a variable inside macro did not get
> initialized, it had no value?

Deja vu?  Didn't you ask this last week or something?

This is no different than regular #set() behavior...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.