byKey and byValue: properties or methods?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 19 10:17:07 PST 2012


On Thu, 19 Jan 2012 12:19:09 -0500, torhu <no at spam.invalid> wrote:

> On 17.01.2012 07:48, Andrei Alexandrescu wrote:
>> I hate I must ask this:
>>
>> int[string] aa;
>> foreach (k; aa.byKey) { ... }
>>
>> or
>>
>> int[string] aa;
>> foreach (k; aa.byKey()) { ... }
>>
>
> For it to be a property, I think you should be able to simplify this  
> example:
>
> ---
> auto k = aa.byKey;
> writeln(k.front);
> k.popFront();
> writeln(k.front);
> ---
>
> to this:
>
> ---
> writeln(k.byKey.front);
> k.byKey.popFront();
> writeln(k.byKey.front);
> ---
>
> and get the same result.  But my understanding is that you wouldn't, in  
> which case byKey doesn't sense to me as a property.  It creates and  
> returns a new range object each time you call it, right?

That's like saying this:

int[] arr;

int l = arr.length;
l++;

should be the same as this:

arr.length++;

because it's a property.

This is an orthogonal problem.  byKey doesn't try to affect the original  
AA, so the semantics should be the same whether you save a copy or access  
it in one line.

There are no hard-fast rules on what should be properties and what  
shouldn't.  But the rvalue vs lvalue is an orthogonal problem.

-Steve


More information about the Digitalmars-d mailing list