User defined properties signatures

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 20 12:03:56 PDT 2015


On 04/20/2015 11:35 AM, dvic wrote:

 > @property string value() { return m_value; } // m_value is a string
 > @property int value() { return to!int(m_value); }

Yes, as Jonathan M Davis said, that's weird.

 > But when using it in writefln() or assert for example, compiler (dmd)
 > complains
 > about 2 different property signatures.
 >
 > What I'd like to have is this:
 >
 > MyObject.value = "12345"; // write property always setting a string
 >
 > assert(MyObject.value == "12345");  // depending on the context, call
 > 1st prop
 > assert(MyObject.value == 12345);  // depending on the context, call 2nd
 > prop

I think that would work if multiple 'alias this' were allowed. (There is 
a pull request that does not pass some tests; that's why it is not 
included in 2.067.)

struct S
{
     int int_value() { /* ... */ }
     string string_value() { /* ... */ }

     alias int_value this;
     alias string_value this;

     // ...
}

The matching function would be called depending on whether S is used in 
place of an int or a string.

Ali



More information about the Digitalmars-d-learn mailing list