Draft RFC: my Dog $spot is just an assertion

2000-09-12 Thread Piers Cawley

=head1 TITLE

C is just an assertion

=head1 VERSION

  Maintainer: Piers Cawley <[EMAIL PROTECTED]>
  Date: 12th September 2000
  Last Modified: 12th September 2000
  Mailing List: [EMAIL PROTECTED]
  Version: 0
  Status: Draft

=head1 ABSTRACT

The behaviour of the  syntax should simply be an
assertion of the invariant: 

   (!defined($spot) || (ref($spot) $spot->isa('Dog)))

=head1 DESCRIPTION

The syntax 

my Dog $spot = Dog->new();

currently carries little weight with Perl, often failing to do what
one expects:

$ perl -wle 'my Dog::$spot; print "ok"' 
No such class Dog at -e line 1, near "my Dog"
Execution of -e aborted due to compilation errors.
$ perl -wle 'sub Dog::new; my Dog $spot; print "ok"'
ok
$ perl -wle 'sub Dog::new; my Dog $spot = 1'
ok

The first example is obvious, as is the second. The third one is
I.  

I therefore propose that C comes to mean that C<$spot>
is restricted to being either undefined or a reference to a C
object (or any subclasses of Dog). Simply having this implicit
assertion can be useful to the programmer, but I would argue that its
main advantage is that the compiler knows the object's interface at
compile time and can potentially use this fact to speed up method
dispatch. 

=head2 Examples

In class methods:

package Dog;
sub speak {
my Dog $self = shift; # Sadly 'my __PACKAGE__ $self' doesn't work
print $self->name, " barks!\n";
}

Admittedly, this makes little sense unless there is some optimization
available, but it can still be useful as a notational convenience.

Or, consider the case where you have an C object and
you're looking to get a Dog from there.

my AnimalShelter $shelter = RSPCA->find_nearest_shelter;
my Dog $stray;

try {
PET: while (!defined($stray)) {
$stray = $shelter->next_animal;
}
}
catch Exception::WrongClass {
next PET;
}
$stray->bark;

Admittedly this makes some assumptions about the possibility of loops
within try blocks, but I think the point is still valid.

My main concern with this proposal is to make it possible to use the
C syntax along with it's associated (posited)
optimizations and assertions wherever it's appropriate in user code. 

=head1 IMPLEMENTATION

I've not really looked into using source filters, but if 
C can be mapped to
C then Tie::Invariant can look
something like:

package Tie::Invariant;
use carp;

sub TIESCALAR {
my $self = bless {value => undef}, shift;
$self->{class} = shift;
return $self;
}

sub FETCH {
my $self = shift;
return $self->value;
}

sub STORE {
my($self,$newval) = @_;

if (!defined($newval) || (ref($newval) &&
  UNIVERSAL::isa($newval, "UNIVERSAL") &&
  $newval->isa($self->{class}))) {
croak "Value must be 'undef' or be a $self->{class}"
}
$self->{value} = $newval;
}

=head1 MIGRATION

Migration issues should be minor, the only problem arising when people
have assigned things that aren't objects of the appropriate type to
typed variables, but they deserve to lose anyway.

=head1 REFERENCES

RFC 171: my Dog $spot should call a constructor implicitly

This RFC is a counter RFC to RFC 171. See my forthcoming
'new pragma: use package' RFC for something that addresses one of the
concerns of RFC 171.

RFC 137: Overview: Perl OO should I be fundamentally changed

My guiding principle.

-- 
Piers


 





Draft RFC: new pragma: C

2000-09-12 Thread Piers Cawley

=head1 TITLE

new pragma: C

=head1 VERSION

Maintainer: Piers Cawley <[EMAIL PROTECTED]>
Date: 12th September 2000
Last Modified: 12th September 2000
Mailing List: [EMAIL PROTECTED]
Version: 0
Status: Draft

=head1 ABSTRACT

Cnew>
is a pain in the bum to type. We should replace this with 

use namespace 'Big::Long::Prefix';
my ::Class $object = ::Class->new;

=head1 DESCRIPTION

There's not that much I can add to the Abstract at the moment, beyond
pointing you all at the p5p thread where I lifted the idea from.

=head1 IMPLEMENTATION

Check out the p5p discussions on C. There's already a
patch against the current perl 5 code to do this.


=head1 REFERENCES

The p5p thread 'RFE: use namespace Foo::Blarg', starting with Alan
Burlison (originator of the idea)'s message
<[EMAIL PROTECTED]>.

The p5p thread 
'[PATCH perl-current] 'use namespace' requires testing', starting
at message <[EMAIL PROTECTED]>, again from Alan Burlison
which looks at the actual patch.

RFC 171: my Dog $spot should call a constructor implicitly

I believe that the issues brought up in RFC 171 are addressed equally
effectively by this RFC, leaving the way clear for my other RFC
suggesting that we leave C pretty much alone.

-- 
Piers




Re: Draft RFC: my Dog $spot is just an assertion

2000-09-12 Thread Damian Conway

Piers wrote:

   > The behaviour of the  syntax should simply be an
   > assertion of the invariant: 
   > 
   >(!defined($spot) || (ref($spot) $spot->isa('Dog)))

(!defined($spot) || (ref($spot) && $spot->isa('Dog')))


Otherwise, AMEN!

Damian