[OT] What are D's values?

Steven Schveighoffer schveiguy at gmail.com
Wed Oct 6 15:59:46 UTC 2021


On 10/4/21 6:15 PM, Walter Bright wrote:

> This is plasticity, the opposite of brittleness.
> 
> What are your experiences with this?

For the most part, it's great!

There is one place where I have struggled though, and D might be able to 
do better. And that is with optional parentheses and taking the address. 
When a property can be either an accessor or a field, then `obj.prop` 
can work the same. However, `&obj.prop` is not the same.

I have solved it by using this stupid function:

```d
auto ref eval(T)(auto ref T t) { return t; }

// instead of &obj.prop
auto ptr = &eval(obj.prop);
```

I just came across this workaround in my code and since I wrote it a 
long time ago it puzzled me "what is this `eval` function?". Took me a 
minute to remember why I did that.

While it's nice D has a mechanism to work around this difference, I find 
having to use such shims a bit awkward. And it's a direct consequence of 
hiding the implementation of a field/property behind the same syntax. 
You can find other cases where D can be awkward (such as 
`typeof(obj.prop)` or checking types regardless of mutability).

What is the "better" answer though? I don't know.

-Steve


More information about the Digitalmars-d mailing list