C is Brittle D is Plastic

Derek Fawcus dfawcus+dlang at employees.org
Sun Mar 22 17:08:05 UTC 2026


On Sunday, 22 March 2026 at 04:47:41 UTC, Walter Bright wrote:
> It's because one cannot switch back and forth between a 
> reference type and a value type without extensively rewriting 
> every use of it. For example:
>
> ```C
> struct S { int a; }
> int foo(struct S s) { return s.a; }
> int bar(struct S *s) { return s->a; }
> ```
> To switch between reference and value, it's necessary to go 
> through all the code swapping . and ->.

Not really, if one has a little for-thought, and commits to 
accessing the members via ->, the two become interchangeable, 
with no performance impact.

```c
int inner1 (struct foo fa) {
         struct foo *fp = &fa;
         fp->a += 1;
         return fp->a + 1;
}
```

One can also adapt in the other direction if one started with 
pointer arguments (add const as desired):

```c
int inner3 (struct foo *fp) {
         struct foo fa = *fp;
         fa.a += 1;
         return fa.a + 1;
}
```

DF


More information about the Digitalmars-d mailing list