ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Re: Customizable default hash and array values.
by David Grove other posts by this author
Sep 25 2001 2:49AM messages near this date
Re: Customizable default hash and array values. | Custom iterators
I think this is one of many steps in the right direction. Actually, I have a 
class item defined in my fork as:

class foo
  reserve bar scalar;
  member bar {
    default(bar) = '1';
    set(bar) = {some code};
    get(bar) = {some code};
    ensure(bar) = {some code};
    confirm(bar) = {some code};
  };
};

Part is stolen from C# and some from ruby and and some from some language I 
saw some time ago but don't remember what it was.

On Monday 24 September 2001 10:06 pm, Michael G Schwern wrote:
>  How many times have you written this (well, since this is Perl 6

>  grammar, you've probably *never* written this, but you get the idea)

> 

>      my $foo = %hash{$key} || 'some default';

> 

>  Annoying, and you've got to scatter this sort of thing all over the

>  code, even if just to avoid "use of uninitialized value" warnings.

> 

>  Wouldn't it be nice if there was a way to change the default value of

>  a hash?  So instead of undef you'll get whatever default you like?

> 

>  Well, if we go through with making hashes and arrays objects under the

>  hood (I'm assuming of classes HASH and ARRAY for the purposes of this

>  discussion) why not just allow:

> 

>      %hash.default('some default');

> 

>  then your code simplifies to:

> 

>      my $foo = %hash{$key};

> 

>  so if $key does not exist you'll get 'some default' instead of undef.

> 

>  The same thing can be applied to arrays.

> 

> 

>  PS  Once again, this is Ruby thievery.



Thread:
Michael G Schwern
RaFaL Pocztarski
Johan Vromans
Bart Lateur
Michael G Schwern
Bart Lateur
Michael G Schwern
Dan Sugalski
David Grove

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved