Properties
Quirin Schroll
qs.il.paperinik at gmail.com
Thu Jul 18 20:31:31 UTC 2024
D’s properties have issues.
The basic idea for this DIP would be to add properties as a new
kind of entity. A property would not be a function as far as the
language is concerned. Asking the type of a property is
meaningful.
A property, essentially, would be a getter–setter pair. It can’t
be overloaded. The address of a property yields a property
pointer or a property delegate, depending if the property is a
non-static member property or not, similar to function pointers
versus delegates.
A property pointer is a pair of function pointers, one pointing
to the getter function and the other to the setter function.
A property delegate is a context pointer plus a property pointer,
three pointers total.
A getter-only property is basically a function. Taking its
address returns a function pointer or a delegate.
Otherwise, as far as syntax is concerned, I’d go the C# route,
with one exception. C# does not allow parameters for properties,
which I would allow.
Example:
```d
struct S
{
size_t property length { get => 0; set { } }
int property prop(string arg)
{
get => 0;
set(int value) { /* use arg and value */ }
}
}
S s;
cast(void)cast(size_t)s.length; // calls getter
cast(void)cast(size_t)s.length(); // error, parens not allowed
s.length = 10; // calls setter
s.length() = 10; // error, parens not allowed
s.prop("abc"); // calls getter
s.prop("abc") = 10; // calls setter
int property delegate() pdg1 = &s.length;
auto l = pdg1(); // indirect getter call
pdg1() = 0; // indirect setter call
int property delegate(string) pdg2 = &s.prop;
auto x = pdg2("abc"); // indirect getter call
pdg2("abc") = 10; // indirect setter call
```
As with function pointers and delegate, calls require parens,
even if empty.
More information about the dip.ideas
mailing list