Skip to content

Commit

Permalink
add PDL::Hash to simplify hash-based PDL subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 24, 2024
1 parent 79522f3 commit db72e96
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Basic/Core/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ anyway. (see L</get_dataref>.)

sub topdl {PDL->topdl(@_)}

####################### Subclass as hashref #######################
{ package # hide from PAUSE
PDL::Hash;
our @ISA = qw/PDL/;
sub initialize {
my ($class) = @_;
bless { PDL => PDL->null }, ref $class || $class;
}
}

####################### Overloaded operators #######################

{ package # hide from MetaCPAN
Expand Down
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- add Primitive::approx_artol
- get_dataref now works even with DONTTOUCHDATA which is really about allocation
- if $pdl->{PDL} is code ref, $pdl now passed as arg
- add PDL::Hash to simplify hash-based PDL subclasses

2.093 2024-09-29
- PDL.set_datatype now doesn't physicalise input, PDL.convert_type does
Expand Down
8 changes: 4 additions & 4 deletions t/subclass.t
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ undef $z;
## First define a PDL-derived object:
{
package PDL::Derived3;
our @ISA = qw/PDL/;
our @ISA = qw/PDL::Hash/;
sub new {
my ($class, $data) = @_;
return $class->SUPER::new($data) if ref($data) ne 'PDL'; # if not object, inherited constructor
Expand All @@ -83,7 +83,7 @@ sub new {
####### Initialize function. This over-ridden function is called by the PDL constructors
sub initialize {
my ($class) = @_;
my $self = bless { PDL => PDL->null }, ref $class || $class;
my $self = $class->SUPER::initialize;
$self->{someThingElse} = 42,
$self;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ $main::OVERRIDEWORKED = 0;
## First define a PDL-derived object:
{
package PDL::Derived4;
our @ISA = qw/PDL/;
our @ISA = qw/PDL::Hash/;
sub new {
my ($class, $data) = @_;
return $class->SUPER::new($data) if ref($data) ne 'PDL'; # if not object, inherited constructor
Expand All @@ -172,7 +172,7 @@ sub new {
sub initialize {
$::INIT_CALLED = 1;
my $class = shift;
my $self = bless { PDL => PDL->null }, ref $class || $class;
my $self = $class->SUPER::initialize;
$self->{someThingElse} = 42,
$self;
}
Expand Down

0 comments on commit db72e96

Please sign in to comment.