DIP23 draft: Fixing properties redux

kenji hara k.hara.pg at gmail.com
Mon Feb 4 06:56:50 PST 2013


2013/2/4 Andrej Mitrovic <andrej.mitrovich at gmail.com>

> On 2/4/13, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> > I'm unclear that's a problem.
>
> The problem is you cannot replace a field with a @property function
> without breaking user-code when you take into account operator
> overloading. Consider:
>

As an essential question, how often occurs rewriting fields to property?

As far as I thought, such rewriting will cause:
1. compile error by type mismatch (if you use &obj.field),
2. or, success of compilation *without any semantic breaking* (if you don't
use &obj.field).

I think the result is not so harmful.


> struct S
> {
>     S opBinary(string op : "&", S)(S s)
>     {
>         return this;
>     }
> }
>
> struct M
> {
>     S s;
> }
>
> void main()
> {
>     M m;
>     auto s = m.s & m.s;  // ok
> }
>
> Suppose you want to turn 's' into a read-only property, so you write:
>
> struct S
> {
>     S opBinary(string op : "&", S)(S s)
>     {
>         return this;
>     }
> }
>
> struct M
> {
>     @property S s() { return _s; }
>     private S _s;
> }
>
> void main()
> {
>     M m;
>     auto s = m.s & m.s;  // fail
> }
>
> Now the user-code fails.
>

This is not correct."m.s & m.s" is always parsed as binary bitwise AND
expression.
So there is no address expression.

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130204/28f67a7d/attachment.html>


More information about the Digitalmars-d mailing list