You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Julio C. Rivera" <ju...@ya.com> on 2003/06/12 17:42:12 UTC

Default value in optional parameters

How can I set a default value for a optional parameter in the specification 
file?

For example:

MyComponent.jwc:
.....
<parameter name="foo" direction="in" type="int" required="no" 
¿¿¿¿¿¿default="20" ?????? />
.....

My question is:
  If I have a optional parameter, how can I use it into a component if I 
don't know if it was initialized?

I know how get it coding get/setFoo() in the MyComponent.java, but I would 
like to do it in a declarative fashion.

Any idea?

Regards.

	Julio.


Re: Default value in optional parameters

Posted by "Julio C. Rivera" <ju...@ya.com>.
Thanks! it helps a lot.
It mean I'm starting to understand tapestry a few ;-).

I'm eager waiting for default value support. It will remove a lot of coding 
work.

Thanks and sorry for my bad english.
         Julio.

At 19:16 12/06/2003 +0300, you wrote:
>[quote]
><parameter name="foo" direction="in" type="int" required="no"
>¿¿¿¿¿¿default="20" ?????? />
>[/quote]
>
>You are ahead of the game. I have started writing the unit tests for that
>just now :)
>
>I believe that will be in one of the next releases, but it has to be
>accepted by the commiters.
>
>In the mean time, you can make the parameter of type 'custom', define
>get/set for the binding (i.e. getParamBinding() and setParamBinding()) and
>implement the preoperty getter in the following way:
>
>Type getParam() {
>     IBinding binding = getParamBinding();
>     if (binding == null)
>         return // default value here//;
>     else
>         return binding.getXXX();
>}
>
>Or sth like that. You can cache the value if you want, but make sure it is
>cleared up when the page detaches (e.g. by having the component implement
>PageDetachListener and cleaning up the cached value in pageDetached().
>
>I hope this helps.
>
>Best regards,
>-mb
>
>
>----- Original Message -----
>From: "Julio C. Rivera" <ju...@ya.com>
>To: <ta...@jakarta.apache.org>
>Sent: Thursday, June 12, 2003 6:42 PM
>Subject: Default value in optional parameters
>
>
>How can I set a default value for a optional parameter in the specification
>file?
>
>For example:
>
>MyComponent.jwc:
>.....
><parameter name="foo" direction="in" type="int" required="no"
>¿¿¿¿¿¿default="20" ?????? />
>.....
>
>My question is:
>   If I have a optional parameter, how can I use it into a component if I
>don't know if it was initialized?
>
>I know how get it coding get/setFoo() in the MyComponent.java, but I would
>like to do it in a declarative fashion.
>
>Any idea?
>
>Regards.
>
>Julio.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Default value in optional parameters

Posted by Mindbridge <mi...@yahoo.com>.
[quote]
<parameter name="foo" direction="in" type="int" required="no"
¿¿¿¿¿¿default="20" ?????? />
[/quote]

You are ahead of the game. I have started writing the unit tests for that
just now :)

I believe that will be in one of the next releases, but it has to be
accepted by the commiters.

In the mean time, you can make the parameter of type 'custom', define
get/set for the binding (i.e. getParamBinding() and setParamBinding()) and
implement the preoperty getter in the following way:

Type getParam() {
    IBinding binding = getParamBinding();
    if (binding == null)
        return // default value here//;
    else
        return binding.getXXX();
}

Or sth like that. You can cache the value if you want, but make sure it is
cleared up when the page detaches (e.g. by having the component implement
PageDetachListener and cleaning up the cached value in pageDetached().

I hope this helps.

Best regards,
-mb


----- Original Message ----- 
From: "Julio C. Rivera" <ju...@ya.com>
To: <ta...@jakarta.apache.org>
Sent: Thursday, June 12, 2003 6:42 PM
Subject: Default value in optional parameters


How can I set a default value for a optional parameter in the specification
file?

For example:

MyComponent.jwc:
.....
<parameter name="foo" direction="in" type="int" required="no"
¿¿¿¿¿¿default="20" ?????? />
.....

My question is:
  If I have a optional parameter, how can I use it into a component if I
don't know if it was initialized?

I know how get it coding get/setFoo() in the MyComponent.java, but I would
like to do it in a declarative fashion.

Any idea?

Regards.

Julio.


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Default value in optional parameters

Posted by Harish Krishnaswamy <hk...@comcast.net>.
Ah, I see the 'int' problem you mean. I agree with you on the simple 
alternative.

Thanks
Harish

Mindbridge wrote:

>HI Harish,
>
>  
>
>>I totally agree that a default parameter is beneficial, but I was
>>pointing out that it could be done easily if one can live with the
>>pitfalls you pointed out.
>>    
>>
>
>Yes, very true.
>
>  
>
>> Although I don't quite understand what the problem is with 'int'.
>>    
>>
>
>Mostly that while with Object it is relatively easy to select a value
>signifying no binding -- null, with int it is more difficult to do so.
>
>  
>
>>Also, can you not use the getFooBinding method to figure out
>>the null binding?
>>    
>>
>
>I guess doing getBinding("Foo") == null would work as well, but it has some
>performance costs. Then again, that's probably not a big deal. It gets a bit
>more messy if you try to cache the value, but it is still quite doable.
>
>So yes, it is not hard to do it at the moment (we are doing it all the time
>at work, in fact :), only requires a bit of coding. In any case, however, it
>would be better to have a simple alternative.
>
>Best regards,
>-mb
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>  
>


Re: Default value in optional parameters

Posted by Mindbridge <mi...@yahoo.com>.
HI Harish,

> I totally agree that a default parameter is beneficial, but I was
> pointing out that it could be done easily if one can live with the
> pitfalls you pointed out.

Yes, very true.

>  Although I don't quite understand what the problem is with 'int'.

Mostly that while with Object it is relatively easy to select a value
signifying no binding -- null, with int it is more difficult to do so.

> Also, can you not use the getFooBinding method to figure out
> the null binding?

I guess doing getBinding("Foo") == null would work as well, but it has some
performance costs. Then again, that's probably not a big deal. It gets a bit
more messy if you try to cache the value, but it is still quite doable.

So yes, it is not hard to do it at the moment (we are doing it all the time
at work, in fact :), only requires a bit of coding. In any case, however, it
would be better to have a simple alternative.

Best regards,
-mb



Re: Default value in optional parameters

Posted by Harish Krishnaswamy <hk...@comcast.net>.
I totally agree that a default parameter is beneficial, but I was 
pointing out that it could be done easily if one can live with the 
pitfalls you pointed out. Although I don't quite understand what the 
problem is with 'int'. May be I am being a novice here. Could you please 
explain? Also, can you not use the getFooBinding method to figure out 
the null binding?

Thanks
Harish


Mindbridge wrote:

>That may work, but there are catches applicable in some cases
>
>    - null may be a valid value passed from the container
>    - the type of the parameter may be sth other than Object. If it is
>'int', for example, how would you write the code below? What would be
>considered as 'no binding' value?
>    - if you want to create 'getFoo()' automatically (type 'auto'), the
>property must be 'required' (for obvious reasons, once you consider the
>above)
>
>So yes, it can be done manually, but it would be much easier, faster, and
>clearer if it can be automatically, it seems. In addition, the 'auto'
>parameters would no longer need to be 'required'.
>
>I hope this makes the need for this clear.
>
>Best regards,
>-mb
>
>
>----- Original Message ----- 
>From: "Harish Krishnaswamy" <hk...@comcast.net>
>To: "Tapestry users" <ta...@jakarta.apache.org>
>Sent: Thursday, June 12, 2003 7:27 PM
>Subject: Re: Default value in optional parameters
>
>
>  
>
>>Why bother with all this, why can't you do a lazy-intialize when you
>>need it. For ex. if you are using foo in renderComponent, let's say,
>>then within renderComponent you may say
>>
>>foo = getFoo();
>>if (foo == null) {
>>    foo = DEFAULTFOO;
>>}
>>
>>// use foo here...
>>
>>-Harish
>>
>>Julio C. Rivera wrote:
>>
>>    
>>
>>>I can set it in my component class, but then I think I have to coding:
>>>
>>>public static int DEFAULTFOO = 20;
>>>private IBinding fooBinding;
>>>
>>>public IBinding getFooBinding() {
>>>        return fooBinding;
>>>}
>>>
>>>public void setFooBinding(IBinding binding) {
>>>        fooBinding = binding;
>>>}
>>>
>>>public int getFoo() {
>>>        if (fooBinding == null)
>>>        {
>>>                return DEFAULTFOO;
>>>        }
>>>        else
>>>        }
>>>                return getFooBinding().getInt()
>>>        }
>>>}
>>>
>>>Is this correct?
>>>
>>>I suppose there is a simpler way, but which?
>>>
>>>
>>>
>>>At 11:46 12/06/2003 -0400, you wrote:
>>>
>>>      
>>>
>>>>Why can't you set it in your component class?
>>>>
>>>>-Harish
>>>>
>>>>Julio C. Rivera wrote:
>>>>
>>>>        
>>>>
>>>>>How can I set a default value for a optional parameter in the
>>>>>specification file?
>>>>>
>>>>>For example:
>>>>>
>>>>>MyComponent.jwc:
>>>>>.....
>>>>><parameter name="foo" direction="in" type="int" required="no"
>>>>>¿¿¿¿¿¿default="20" ?????? />
>>>>>.....
>>>>>
>>>>>My question is:
>>>>> If I have a optional parameter, how can I use it into a component
>>>>>if I don't know if it was initialized?
>>>>>
>>>>>I know how get it coding get/setFoo() in the MyComponent.java, but I
>>>>>would like to do it in a declarative fashion.
>>>>>
>>>>>Any idea?
>>>>>
>>>>>Regards.
>>>>>
>>>>>    Julio.
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>>
>>>>>          
>>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>
>>>>        
>>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>>
>>>      
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>  
>


Re: Default value in optional parameters

Posted by Mindbridge <mi...@yahoo.com>.
That may work, but there are catches applicable in some cases

    - null may be a valid value passed from the container
    - the type of the parameter may be sth other than Object. If it is
'int', for example, how would you write the code below? What would be
considered as 'no binding' value?
    - if you want to create 'getFoo()' automatically (type 'auto'), the
property must be 'required' (for obvious reasons, once you consider the
above)

So yes, it can be done manually, but it would be much easier, faster, and
clearer if it can be automatically, it seems. In addition, the 'auto'
parameters would no longer need to be 'required'.

I hope this makes the need for this clear.

Best regards,
-mb


----- Original Message ----- 
From: "Harish Krishnaswamy" <hk...@comcast.net>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Thursday, June 12, 2003 7:27 PM
Subject: Re: Default value in optional parameters


> Why bother with all this, why can't you do a lazy-intialize when you
> need it. For ex. if you are using foo in renderComponent, let's say,
> then within renderComponent you may say
>
> foo = getFoo();
> if (foo == null) {
>     foo = DEFAULTFOO;
> }
>
> // use foo here...
>
> -Harish
>
> Julio C. Rivera wrote:
>
> > I can set it in my component class, but then I think I have to coding:
> >
> > public static int DEFAULTFOO = 20;
> > private IBinding fooBinding;
> >
> > public IBinding getFooBinding() {
> >         return fooBinding;
> > }
> >
> > public void setFooBinding(IBinding binding) {
> >         fooBinding = binding;
> > }
> >
> > public int getFoo() {
> >         if (fooBinding == null)
> >         {
> >                 return DEFAULTFOO;
> >         }
> >         else
> >         }
> >                 return getFooBinding().getInt()
> >         }
> > }
> >
> > Is this correct?
> >
> > I suppose there is a simpler way, but which?
> >
> >
> >
> > At 11:46 12/06/2003 -0400, you wrote:
> >
> >> Why can't you set it in your component class?
> >>
> >> -Harish
> >>
> >> Julio C. Rivera wrote:
> >>
> >>> How can I set a default value for a optional parameter in the
> >>> specification file?
> >>>
> >>> For example:
> >>>
> >>> MyComponent.jwc:
> >>> .....
> >>> <parameter name="foo" direction="in" type="int" required="no"
> >>> ¿¿¿¿¿¿default="20" ?????? />
> >>> .....
> >>>
> >>> My question is:
> >>>  If I have a optional parameter, how can I use it into a component
> >>> if I don't know if it was initialized?
> >>>
> >>> I know how get it coding get/setFoo() in the MyComponent.java, but I
> >>> would like to do it in a declarative fashion.
> >>>
> >>> Any idea?
> >>>
> >>> Regards.
> >>>
> >>>     Julio.
> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >>>
> >>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> >> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Default value in optional parameters

Posted by Harish Krishnaswamy <hk...@comcast.net>.
Why bother with all this, why can't you do a lazy-intialize when you 
need it. For ex. if you are using foo in renderComponent, let's say, 
then within renderComponent you may say

foo = getFoo();
if (foo == null) {
    foo = DEFAULTFOO;
}

// use foo here...

-Harish

Julio C. Rivera wrote:

> I can set it in my component class, but then I think I have to coding:
>
> public static int DEFAULTFOO = 20;
> private IBinding fooBinding;
>
> public IBinding getFooBinding() {
>         return fooBinding;
> }
>
> public void setFooBinding(IBinding binding) {
>         fooBinding = binding;
> }
>
> public int getFoo() {
>         if (fooBinding == null)
>         {
>                 return DEFAULTFOO;
>         }
>         else
>         }
>                 return getFooBinding().getInt()
>         }
> }
>
> Is this correct?
>
> I suppose there is a simpler way, but which?
>
>
>
> At 11:46 12/06/2003 -0400, you wrote:
>
>> Why can't you set it in your component class?
>>
>> -Harish
>>
>> Julio C. Rivera wrote:
>>
>>> How can I set a default value for a optional parameter in the 
>>> specification file?
>>>
>>> For example:
>>>
>>> MyComponent.jwc:
>>> .....
>>> <parameter name="foo" direction="in" type="int" required="no" 
>>> ¿¿¿¿¿¿default="20" ?????? />
>>> .....
>>>
>>> My question is:
>>>  If I have a optional parameter, how can I use it into a component 
>>> if I don't know if it was initialized?
>>>
>>> I know how get it coding get/setFoo() in the MyComponent.java, but I 
>>> would like to do it in a declarative fashion.
>>>
>>> Any idea?
>>>
>>> Regards.
>>>
>>>     Julio.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>



Re: Default value in optional parameters

Posted by "Julio C. Rivera" <ju...@ya.com>.
I can set it in my component class, but then I think I have to coding:

public static int DEFAULTFOO = 20;
private IBinding fooBinding;

public IBinding getFooBinding() {
         return fooBinding;
}

public void setFooBinding(IBinding binding) {
         fooBinding = binding;
}

public int getFoo() {
         if (fooBinding == null)
         {
                 return DEFAULTFOO;
         }
         else
         }
                 return getFooBinding().getInt()
         }
}

Is this correct?

I suppose there is a simpler way, but which?



At 11:46 12/06/2003 -0400, you wrote:
>Why can't you set it in your component class?
>
>-Harish
>
>Julio C. Rivera wrote:
>
>>How can I set a default value for a optional parameter in the 
>>specification file?
>>
>>For example:
>>
>>MyComponent.jwc:
>>.....
>><parameter name="foo" direction="in" type="int" required="no" 
>>¿¿¿¿¿¿default="20" ?????? />
>>.....
>>
>>My question is:
>>  If I have a optional parameter, how can I use it into a component if I 
>> don't know if it was initialized?
>>
>>I know how get it coding get/setFoo() in the MyComponent.java, but I 
>>would like to do it in a declarative fashion.
>>
>>Any idea?
>>
>>Regards.
>>
>>     Julio.
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


Re: Default value in optional parameters

Posted by Harish Krishnaswamy <hk...@comcast.net>.
Why can't you set it in your component class?

-Harish

Julio C. Rivera wrote:

> How can I set a default value for a optional parameter in the 
> specification file?
>
> For example:
>
> MyComponent.jwc:
> .....
> <parameter name="foo" direction="in" type="int" required="no" 
> ¿¿¿¿¿¿default="20" ?????? />
> .....
>
> My question is:
>  If I have a optional parameter, how can I use it into a component if 
> I don't know if it was initialized?
>
> I know how get it coding get/setFoo() in the MyComponent.java, but I 
> would like to do it in a declarative fashion.
>
> Any idea?
>
> Regards.
>
>     Julio.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>