Proposal: property 'fetch' for AA
eao197
eao197 at intervale.ru
Thu May 3 22:53:53 PDT 2007
Ruby's Hash has a handy method 'fetch'[1] which allows to extract value
from Hash or, if value is missed, use some default value:
irb(main):001:0> h = { :a => 1 }
=> {:a=>1}
irb(main):002:0> a = h.fetch(:a, 0)
=> 1
irb(main):003:0> b = h.fetch(:b, 10)
=> 10
If D's AA would have property 'fetch' it would allow to write:
int[char] h;
h[ 'a' ] = 1;
auto a = h.fetch( 'a', 0 );
auto b = h.fetch( 'b', 10 );
instead of:
V fetch(K,V)( V[K] h, K key, V default_value )
{
if( key in h )
return h[key];
return default_value;
}
void
main()
{
int[char] h;
h[ 'a' ] = 1;
auto a = h.fetch( 'a', 0 );
auto b = h.fetch( 'b', 10 );
}
I think it is better to have 'fetch' implementation in the language or the
standard library.
[1] http://www.ruby-doc.org/core/classes/Hash.html#M002877
--
Regards,
Yauheni Akhotnikau
More information about the Digitalmars-d
mailing list