Re: Default values for undef attributes

2011-02-15 Thread WOLfgang Schricker
Octavian Rasnita schrieb:
> Hi,
[...]
> package TestMod;
> 
> use Moose;
> 
> has foo => (is => 'ro', isa => 'Int', default => 123);
> 
> __PACKAGE__->meta->make_immutable;
> 
> package main;
> 
> use strict;
[...]
> #This doesn't work because foo is undefined:
> my $t = TestMod->new(foo => undef);
> print $t->foo;
> 
package TestMod;

use Moose;

has foo => (is => 'ro', isa => 'Int|Undef', default => 123);

__PACKAGE__->meta->make_immutable;

package main;

use 5.012;
use Data::Dumper;

my $test_0 = TestMod->new();
my $test_1 = TestMod->new(foo => 456);
my $test_2 = TestMod->new(foo => undef);

say Dumper($test_0, $test_1, $test_2);

See 'isa' ;-)
-- 
Regards
*WOL* fgang *S* chricker



Re: Default values for undef attributes

2011-01-29 Thread Octavian Rasnita
From: "Hans Dieter Pearcey" 
> On Sat, 29 Jan 2011 09:23:45 +0200, "Octavian Rasnita"  
> wrote:
>> I want to use some attributes that get a default value when they are not 
>> sent as constructor's parameters and also when they are sent as constructor 
>> parameters but with an undef value.
> 
> Use MooseX::UndefTolerant.  The undef values will be ignored and the normal
> default/builder mechanism will kick in.

Thank you! I din't know about it.

> A coercion is wrong, since that will be per-type, not per-attribute or
> per-class.

In that case I would need to define a subtype for each integer that has a 
different default value, considering that there is a subtype of Int that has 
the default value of 0, another subtype of Int that has a default value of 1, 
and so on. This would be much harder, of course.

Thanks again.

Octavian







Re: Default values for undef attributes

2011-01-29 Thread Mike Raynham

On 29/01/11 13:20, Hans Dieter Pearcey wrote:

On Sat, 29 Jan 2011 09:23:45 +0200, "Octavian Rasnita"  
wrote:

I want to use some attributes that get a default value when they are not sent 
as constructor's parameters and also when they are sent as constructor 
parameters but with an undef value.


Use MooseX::UndefTolerant.  The undef values will be ignored and the normal
default/builder mechanism will kick in.

A coercion is wrong, since that will be per-type, not per-attribute or
per-class.

hdp.


That looks very useful.  Thanks for the pointer.


Re: Default values for undef attributes

2011-01-29 Thread Hans Dieter Pearcey
On Sat, 29 Jan 2011 09:23:45 +0200, "Octavian Rasnita"  
wrote:
> I want to use some attributes that get a default value when they are not sent 
> as constructor's parameters and also when they are sent as constructor 
> parameters but with an undef value.

Use MooseX::UndefTolerant.  The undef values will be ignored and the normal
default/builder mechanism will kick in.

A coercion is wrong, since that will be per-type, not per-attribute or
per-class.

hdp.


Re: Default values for undef attributes

2011-01-29 Thread Mike Raynham

On 29/01/11 07:23, Octavian Rasnita wrote:

Hi,

I want to use some attributes that get a default value when they are not sent 
as constructor's parameters and also when they are sent as constructor 
parameters but with an undef value.

I have gave an example below and shown that the third way of constructing the 
object doesn't work because the value undef is not an Int.

Which should be the recommended way to do this? The program wouldn't give errors if the 
attribute isa "maybe[Int]" but I feel that this isn't the right way to do that, 
and even in that case, the default value is still not set, so the program doesn't work 
correctly.

Is there a coercion required?

Thanks.

package TestMod;

use Moose;

has foo =>  (is =>  'ro', isa =>  'Int', default =>  123);

__PACKAGE__->meta->make_immutable;

package main;

use strict;

#This works; foo gets the default value:
my $t = TestMod->new;
print $t->foo;

#This also works; foo gets the provided value:
my $t = TestMod->new(foo =>  321);
print $t->foo;

#This doesn't work because foo is undefined:
my $t = TestMod->new(foo =>  undef);
print $t->foo;

Octavian



I'm quite new to this, so I don't know if this is the best or correct 
way to do it, but yes, you can use coercion to coerce from undef:


package TestMod;

use Moose;
use Moose::Util::TypeConstraints;

subtype 'TestMod::Int' => as 'Int';

coerce 'TestMod::Int'
=> from 'Undef'
=> via { 123; };

has foo => (
is  => 'ro',
isa => 'TestMod::Int',
default => '123',
coerce  => 1,
);

__PACKAGE__->meta->make_immutable;

package main;

use strict;
use warnings;

# Returns 123
my $t = TestMod->new();
print $t->foo . "\n";

# Returns 321
$t = TestMod->new(foo => 321);
print $t->foo . "\n";

# Returns 123
$t = TestMod->new(foo => undef);
print $t->foo . "\n";

1;



Default values for undef attributes

2011-01-29 Thread Octavian Rasnita
Hi,

I want to use some attributes that get a default value when they are not sent 
as constructor's parameters and also when they are sent as constructor 
parameters but with an undef value.

I have gave an example below and shown that the third way of constructing the 
object doesn't work because the value undef is not an Int.

Which should be the recommended way to do this? The program wouldn't give 
errors if the attribute isa "maybe[Int]" but I feel that this isn't the right 
way to do that, and even in that case, the default value is still not set, so 
the program doesn't work correctly.

Is there a coercion required?

Thanks.

package TestMod;

use Moose;

has foo => (is => 'ro', isa => 'Int', default => 123);

__PACKAGE__->meta->make_immutable;

package main;

use strict;

#This works; foo gets the default value:
my $t = TestMod->new;
print $t->foo;

#This also works; foo gets the provided value:
my $t = TestMod->new(foo => 321);
print $t->foo;

#This doesn't work because foo is undefined:
my $t = TestMod->new(foo => undef);
print $t->foo;

Octavian