Jump to page: 1 2
Thread overview
properties - Why not the set/get syntax?
Apr 01, 2006
Cris
Apr 01, 2006
John C
Apr 01, 2006
Cris
Apr 01, 2006
John C
Apr 01, 2006
Cris
Apr 01, 2006
Cris
Apr 01, 2006
John C
Apr 01, 2006
Cris
Apr 01, 2006
John C
Apr 01, 2006
Carlos Santander
Apr 02, 2006
Derek Parnell
Apr 02, 2006
Kyle Furlong
Apr 02, 2006
Søren J. Løvborg
April 01, 2006
Why the properties in D do not use the set/get syntax? It looks more clear and natural to me.
April 01, 2006
"Cris" <central_p@hotmail.com> wrote in message news:e0lg9c$27q4$1@digitaldaemon.com...
> Why the properties in D do not use the set/get syntax? It looks more clear and natural to me.

Which syntax? There are many. C#, VB, Delphi, C++/CLI, Visual C++ ... take your pick. (But none exists for Java - purists would tell you it's just so very wrong.)

This comes up every few months. So that we don't rehash the same arguments, here's the previous thread on this topic: http://www.digitalmars.com/d/archives/digitalmars/D/31802.html

John C.


April 01, 2006
How would you implement a  write only property?

So if you want ot have a read and write property you just have to add one read and one write property?

> struct Foo
> {
>     int data() { return m_data; }	// read property
> 
>     int data(int value) { return m_data = value; } // write property
> 
>   private:
>     int m_data;
> }

C#'s syntax feels a little bit more clear to me. I'm reading now the link you've posted before.
April 01, 2006
"Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
> How would you implement a  write only property?

Implement only the setter.

>
> So if you want ot have a read and write property you just have to add one read and one write property?

Correct.

>
>> struct Foo
>> {
>>     int data() { return m_data; } // read property
>>
>>     int data(int value) { return m_data = value; } // write property
>>
>>   private:
>>     int m_data;
>> }
>
> C#'s syntax feels a little bit more clear to me. I'm reading now the link you've posted before.

I agree it's a lot nicer. But many think it's too verbose. I'd be willing to sacrifice succinctness for clarity.


April 01, 2006
John C wrote:
> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>> How would you implement a  write only property?
> 
> Implement only the setter.

I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?
April 01, 2006
Cris wrote:
> John C wrote:
>> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>>> How would you implement a  write only property?
>>
>> Implement only the setter.
> 
> I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?


Oh, yes it's possible:

class Bicycle
{
    this()
    {
        gear = 1;
    }

    int currentGear;

    int gear()
    {
        return currentGear;
    }

    void gear(int value)
    {
        currentGear = value;
    }
}

So it means that there is absolyutely no difference between properties and functions in D? Perhaps the notion "property" is a little bit confusing in this case.
April 01, 2006
"Cris" <central_p@hotmail.com> wrote in message news:e0m0an$2sd1$1@digitaldaemon.com...
> John C wrote:
>> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>>> How would you implement a  write only property?
>>
>> Implement only the setter.
>
> I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?

As I said, implement only the setter.

struct Foo {
    int data_;
    void data(int value) { data_ = value; }
}

void main() {
    Foo foo;
    foo.data = 10;
    int a = foo.data; // function main.Foo.data (int) does not match
argument types.
}


April 01, 2006
"Cris" <central_p@hotmail.com> wrote in message news:e0m0lc$2slj$1@digitaldaemon.com...
> Cris wrote:
>> John C wrote:
>>> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>>>> How would you implement a  write only property?
>>>
>>> Implement only the setter.
>>
>> I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?
>
>
> Oh, yes it's possible:
>
> class Bicycle
> {
>     this()
>     {
>         gear = 1;
>     }
>
>     int currentGear;
>
>     int gear()
>     {
>         return currentGear;
>     }
>
>     void gear(int value)
>     {
>         currentGear = value;
>     }
> }

But Bicycle.gear can be both read and written. I showed you in another reply how to do a write-only property.

>
> So it means that there is absolyutely no difference between properties and functions in D? Perhaps the notion "property" is a little bit confusing in this case.

This is why people keep suggesting an alternative syntax so that the compiler distinguishes between them. But it works most of the time, unless you're using it with auto type inference (in which case you need to use the parentheses).


April 01, 2006
Thank you for your relies, John!


John C wrote:
> "Cris" <central_p@hotmail.com> wrote in message news:e0m0lc$2slj$1@digitaldaemon.com...
>> Cris wrote:
>>> John C wrote:
>>>> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>>>>> How would you implement a  write only property?
>>>> Implement only the setter.
>>> I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?
>>
>> Oh, yes it's possible:
>>
>> class Bicycle
>> {
>>     this()
>>     {
>>         gear = 1;
>>     }
>>
>>     int currentGear;
>>
>>     int gear()
>>     {
>>         return currentGear;
>>     }
>>
>>     void gear(int value)
>>     {
>>         currentGear = value;
>>     }
>> }
> 
> But Bicycle.gear can be both read and written. I showed you in another reply how to do a write-only property.
> 
>> So it means that there is absolyutely no difference between properties and functions in D? Perhaps the notion "property" is a little bit confusing in this case.
> 
> This is why people keep suggesting an alternative syntax so that the compiler distinguishes between them. But it works most of the time, unless you're using it with auto type inference (in which case you need to use the parentheses). 
> 
> 
April 01, 2006
John C escribió:
> "Cris" <central_p@hotmail.com> wrote in message news:e0m0an$2sd1$1@digitaldaemon.com...
>> John C wrote:
>>> "Cris" <central_p@hotmail.com> wrote in message news:e0lr0p$2n17$1@digitaldaemon.com...
>>>> How would you implement a  write only property?
>>> Implement only the setter.
>> I mean a property that you can write but you cannot read. You can do that in C#. Is it possible in D too?
> 
> As I said, implement only the setter.
> 
> struct Foo {
>     int data_;
>     void data(int value) { data_ = value; }
> }
> 
> void main() {
>     Foo foo;
>     foo.data = 10;
>     int a = foo.data; // function main.Foo.data (int) does not match argument types.
> } 
> 
> 

Maybe that error message is another reason to have a true property syntax. For someone new to D expecting data to be a property, that message is as puzzling as it gets.

-- 
Carlos Santander Bernal
« First   ‹ Prev
1 2