Jump to page: 1 2 3
Thread overview
What is the state of @property?
Aug 25, 2022
Ki Rill
Aug 25, 2022
Ki Rill
Aug 25, 2022
bauss
Aug 28, 2022
Salih Dincer
Aug 28, 2022
Salih Dincer
Aug 29, 2022
Simen Kjærås
Aug 29, 2022
Guillaume Piolat
Aug 29, 2022
rikki cattermole
Aug 29, 2022
Salih Dincer
Aug 25, 2022
Andrey Zherikov
Aug 25, 2022
bauss
Aug 25, 2022
Salih Dincer
Aug 26, 2022
frame
Aug 26, 2022
Salih Dincer
Aug 25, 2022
Mike Parker
Aug 25, 2022
Andrey Zherikov
Aug 25, 2022
rikki cattermole
Aug 29, 2022
zako
Sep 02, 2022
cc
August 25, 2022

It's not recommended to use it, yet people do continue using it anyway... I see it in people's code. So, was it finalized, but docs weren't updated? Or is it still half-backed? And what is the issue?

August 24, 2022

On 8/24/22 11:26 PM, Ki Rill wrote:

>

It's not recommended to use it, yet people do continue using it anyway... I see it in people's code. So, was it finalized, but docs weren't updated? Or is it still half-backed? And what is the issue?

It's not even 1/10 baked.

It does just about nothing. The only discernable difference is typeof:

struct S
{
    int prop1() { return 1; }
    @property int prop2() { return 2; }
}

pragma(msg, typeof(S.prop1)); // int()
pragma(msg, typeof(S.prop2)); // int

What is the issue? The attribute was added with the intention that properties would require a @property attribute, and functions would not. But there was pushback, so it never got formalized. For a while we had a -property switch which would enforce some rules, but it was also removed.

As of today, I'd ignore @property. If someone is using it, just pretend it's a normal function.

-Steve

August 25, 2022

On Thursday, 25 August 2022 at 03:49:07 UTC, Steven Schveighoffer wrote:

>

As of today, I'd ignore @property. If someone is using it, just pretend it's a normal function.

-Steve

Thank you, Steven, for the detailed reply. I will continue ignoring it. Anyways, we have inout functions, and the functionality is the same.

August 25, 2022

On Thursday, 25 August 2022 at 03:49:07 UTC, Steven Schveighoffer wrote:

>

On 8/24/22 11:26 PM, Ki Rill wrote:

>

It's not recommended to use it, yet people do continue using it anyway... I see it in people's code. So, was it finalized, but docs weren't updated? Or is it still half-backed? And what is the issue?

It's not even 1/10 baked.

It does just about nothing. The only discernable difference is typeof:

struct S
{
    int prop1() { return 1; }
    @property int prop2() { return 2; }
}

pragma(msg, typeof(S.prop1)); // int()
pragma(msg, typeof(S.prop2)); // int

What is the issue? The attribute was added with the intention that properties would require a @property attribute, and functions would not. But there was pushback, so it never got formalized. For a while we had a -property switch which would enforce some rules, but it was also removed.

As of today, I'd ignore @property. If someone is using it, just pretend it's a normal function.

-Steve

Personally I use it for documentation as it makes it clear what the intentions are of said functions.

August 25, 2022

On Thursday, 25 August 2022 at 03:26:32 UTC, Ki Rill wrote:

>

It's not recommended to use it, yet people do continue using it anyway... I see it in people's code. So, was it finalized, but docs weren't updated? Or is it still half-backed? And what is the issue?

Why is it not recommended?
It's useful when you need to evolve API and migrate users from one data member to another, for example:

struct S
{
  @property string old_member() const { return new_member; }

  string new_member;
}

Or when you want to do some audit:

struct S
{
  @property string member() const
  {
    writeln("'member' is used by ", environment["USER"]);
    return _member;
  }

  string _member;
}

I have no objections to deprecate @property attribute but I believe D must support the use case when obj.mem expression calls obj.mem() function (this is usually called property functions).

August 25, 2022

On Thursday, 25 August 2022 at 13:18:38 UTC, Andrey Zherikov wrote:

>

On Thursday, 25 August 2022 at 03:26:32 UTC, Ki Rill wrote:

>

It's not recommended to use it, yet people do continue using it anyway... I see it in people's code. So, was it finalized, but docs weren't updated? Or is it still half-backed? And what is the issue?

Why is it not recommended?
It's useful when you need to evolve API and migrate users from one data member to another, for example:

struct S
{
  @property string old_member() const { return new_member; }

  string new_member;
}

Or when you want to do some audit:

struct S
{
  @property string member() const
  {
    writeln("'member' is used by ", environment["USER"]);
    return _member;
  }

  string _member;
}

I have no objections to deprecate @property attribute but I believe D must support the use case when obj.mem expression calls obj.mem() function (this is usually called property functions).

I'm really not sure what you mean.

Currently @property doesn't really do anything in this regard, so I imagine that the code would work the same without @property, no?

August 25, 2022

On Thursday, 25 August 2022 at 13:18:38 UTC, Andrey Zherikov wrote:

>

I have no objections to deprecate @property attribute but I believe D must support the use case when obj.mem expression calls obj.mem() function (this is usually called property functions).

That works by default without @property, and has since way, way back in the early D1 days.

August 26, 2022
On 26/08/2022 1:18 AM, Andrey Zherikov wrote:
> I have no objections to deprecate `@property` attribute but I believe D must support the use case when `obj.mem` expression calls `obj.mem()` function (this is usually called property functions).

It already does. That is the problem!

```d
import std;
void main()
{
    S s;
    s.new_member = "foo";
    s.old_member.writeln;
}

struct S
{
  string old_member() { return new_member; }
  string new_member;
}
```
August 25, 2022

On Thursday, 25 August 2022 at 13:25:58 UTC, Mike Parker wrote:

>

On Thursday, 25 August 2022 at 13:18:38 UTC, Andrey Zherikov wrote:

>

I have no objections to deprecate @property attribute but I believe D must support the use case when obj.mem expression calls obj.mem() function (this is usually called property functions).

That works by default without @property, and has since way, way back in the early D1 days.

I didn't know that! Shame on me 🤦

August 25, 2022

On Thursday, 25 August 2022 at 13:23:25 UTC, bauss wrote:

>

Currently @property doesn't really do anything in this regard, so I imagine that the code would work the same without @property, no?

One benefit of @property is that it allows the compiler to infer automatically. So it behaves like auto but superior! For example:

import std.stdio;

struct ReverseS(int size)
{
  private char[size] bytes;
  private int index = size - 1;

  @property empty() {
    return index < 0;
  }

  @property front() {
    return bytes[index];
  }

  @property popFront() {
    --index;
  }
}

void main()
{
  char[9] text = "ytreporp@".dup;

  foreach(c; ReverseS!9(text)) {
    c.write;
  }
  writeln; // @property
}

SDB@79

« First   ‹ Prev
1 2 3