[% setvar title Add new C keyword to DWIM for clearing values %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Add new empty keyword to DWIM for clearing values

VERSION

  Maintainer: Nathan Wiger <nate@wiger.org>
  Date: 16 Sep 2000
  Last-Modified: 29 Sep 2000
  Mailing List: perl6-language@perl.org
  Number: 245
  Version: 2
  Status: Retracted

ABSTRACT

Most beginners and even intermediate users expect this:

   my @array = undef;
   undef @array;

To initialize an empty array. It doesn't, and shouldn't. This RFC proposes a new keyword, empty, which does the "right thing", and can be used as both a noun and verb:

   my @array = empty;
   empty @array;

The idea is to make this process more intuitive. It also makes a few things easier.

NOTES ON RETRACTION

Hey, they're not all gonna be winners, people! ;-)

DESCRIPTION

Currently, initializing values so that warnings are spewed out or confusion doesn't arise is not as easy as it could be. These:

   my $name = undef;
   my($a, $b, $c, $d, $e, $f) = undef;
   my(@name, $age, %attrs) = undef;

Don't do what many (most?) people expect them to. Getting the corresponding effects that most people want can be cumbersome and not always intuitive:

   my $name = '';
   my($a, $b, $c, $d, $e, $f) = ( '' x 6 );
   my @name = (); my $age = ''; my %attrs = (); 

With the new empty keyword, this becomes easier and more readable, with a nice parallel to undef:

   my $name = empty;
   my($a, $b, $c, $d, $e, $f) = empty;
   my(@name, $age, %attrs) = empty;

Like undef, it also has the benefit of being usable on values themselves as a verb:

   empty @array;          # @array = ();
   empty %hash;           # %hash = ();
   empty @array, $name;   # @array = (); $name = '';

And its purpose is intuitive: It causes a value to become empty, without being undefined. Note that it can also shorten code in some situations.

NOTES

I'm throwing this out there. It seems to me to make easy things easier, and seems to potentially make the teaching of undef easier as well, since the usage of undef and empty are parallel. The point of debate I'm sure will be whether or not it hides implicit contexts, but it doesn't seem that it would have to assuming that it was properly integrated into the undef/context part of a course.

Nonetheless, feel free to point out why this is a bad idea and I'll gladly retract it.

IMPLEMENTATION

See here in v2, assuming this doesn't get ripped apart.

MIGRATION

None. New functionality.

REFERENCES

www.mail-archive.com

And a previous, humorous yet poignant one from Tom C which appears to have vanished