Properties: problems

John C johnch_atms at hotmail.com
Thu Jul 30 01:57:13 PDT 2009


Chad J wrote:
> John C wrote:
>> Here's a couple of annoying problems I encounter quite often with D's
>> properties. Would having some form of property syntax fix them?
>>
>> 1) Array extensions:
>>
>>   class Person {
>>
>>     string name_;
>>
>>     string name() {
>>       return name_;
>>     }
>>
>>   }
>>
>>   auto person = getPerson();
>>   auto firstAndLast = person.name.split(' ');
>>
>> The above line currently requires parentheses after 'name' to compile.
>>
> 
> This one is weird.  After defining getPerson() I was able to rewrite the
> last line into this and make it compile:
> 
> 	auto firstAndLast = split(person.name," ");

Yes, that's D's special array syntax, where free functions can be called 
as if they were "methods" of an array.

> 
> Note that you need qoutes, not just ' '.

My mistake.

> But even
> 
> 	auto firstAndLast = person.name.split(" ");
> 
> does not compile.
> main2.d(36): Error: function expected before (), not
> split(person.name()) of type immutable(char)[][]
> 
> This is probably a compiler bug.

This is my point. The compiler can't tell that "name" is a property, so 
it expects parentheses "name()" to work. That's the problem.

> 
> I don't think property syntax is truly necessary for this example.  You
> are fortunate enough to be using strings, which are passed by reference.

Sorry, I don't see how this statement is relevant at all.



More information about the Digitalmars-d mailing list