Hi all. Whilst investigating Tangram, a thought occurred to me: wouldn't it be great to be able to specify the exact attributes that an object is allowed to have in the class definition, and have "standard" constructors, access and update methods use those as a master source for their actions?
You have to tell Tangram what attributes an object has, so that it knows how to store it in the database. So why not combine the two? Tao::Object facilitates just that. For instance, say you have a "Person" class that has attributes "name", "age" and "father". That would be represented in a Tangram schema as:
package Person; $schema = { fields => { string => [ qw(name profession) ], int => [ qw(age) ], ref => [ qw(father) ], } };
It's a simple data type, so what do you want to check, other than the name is a string short enough to go in the database, the age is an integer and "father"->isa "Person"?
Adding Tao::Object to @Person::ISA, you would be able to do the following without writing any class functions whatsoever.
my $father = new Person(name => "Charlie Vilain", age => 49, profession => "DMS II Guru"); my $self = new Person(name => "Sam Vilain", age => 22, profession => "Perl weenie", father => $father); print "My name is... " . $self->name() . "\n"; $self->name("Samuel"); # ok, sets name to "Samuel" $self->age("old"); # croaks, age is an int $self->father("Darth"); # croaks, "Darth" is not a blessed object print $self->mother(); # croaks, "mother" not recorded in schema
Now, I don't know about you lot, but I am finding this shortcut bloody useful, and it doesn't feel like a strict type straightjacket.
The meditiation is this: do you think that this is a useful shortcut, or will promote sloppy programming? Note: you can supply a custom function to check each attribute, and the destructor will automatically delete $obj->{father}, because it's a reference and that's generally a good idea to break potential circular references. See the POD for more.
In the Perl 6 PDF by Damian Conway, it mentions "More Declarative class specifications". Does anyone know offhand whether that will cover this sort of thing, or any other modules that I've missed that cover this sort of area?
I would also appreciate any peer review of the code, etc. If I get enough positive feedback from this, I may have to polish it up and CPAN it.
I can see other really useful uses for this, such as automatically generating a dia XML file from a set of module files that is a UML diagram representing your project. That would be ultra-cool, but would need some development to work.
srand 3.14159; print join("", sort{rand 1<0.5}map{$_^"\037"}split m{ }x,"qmptk|z~wOzm??l]pUqx^k?j"),",\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Too lazy for constructors
by bikeNomad (Priest) on Jul 18, 2001 at 19:01 UTC | |
by mugwumpjism (Hermit) on Jul 18, 2001 at 19:34 UTC | |
|
Re: Too lazy for constructors
by frag (Hermit) on Jul 18, 2001 at 18:33 UTC | |
by mugwumpjism (Hermit) on Jul 18, 2001 at 20:07 UTC | |
|
Re: Too lazy for constructors
by Malkavian (Friar) on Jul 18, 2001 at 17:18 UTC | |
|
Re: Too lazy for constructors
by Sinister (Friar) on Jul 18, 2001 at 15:32 UTC |