Hacker News new | ask | show | jobs
by WalterBright 10 days ago
Over the many years I was writing code in C, I noticed that the original program design and data structures were very persistent, i.e. they were never reorganized. I suspect the cause is the tendrils of those choices found their way into everything, so reorganizing it was thoroughly impractical.

One of the problems, for C, is the use of . to access a field via a variable, and -> to access a field via a pointer. To switch between a pointer and a variable in a struct means you've got to re-edit large quantities of code everywhere. The result is just too tedious.

This was the motivation in D for using . for both. It turns out, it is completely unnecessary for having both . and -> as the compiler semantic pass can trivially figure it out. Eliminating this issue makes for much easier redesign of a data structure.

2 comments

I actually don't hate that part of C. You have to acknowledge it's a pointer with a different accessor. I liken it to = vs := in Go. It's an unnecessary semantic, sure, but it elevates a technical detail to be part of the syntax of the language. Go has caught me a couple of times when I forgot I had the same variable name in scope already (e.g. opening a second file descriptor and trying to name it "fd" again).

My mind equates -> to "this could be NULL" without ever looking at the function signature.

It is way more of a pain in the ass for refactors than = vs := though.

I don’t think that’s a good comparison. It’s to do with variable scoping, which is really quite core to reading and comprehending code.

-> just… isn’t. Acknowledging it’s a pointer with a different accessor is just not really that important. I mean, in some specific cases it might be, but in general it’s not.

Point in case: go?

There's one more aspect to it in D:

    struct S {
      int a;
      int f(); //getter
      void f(int i); // setter
    }
    void test() {
      S s;
      int i = s.a;
      int j = s.f;
      s.f = i;

      S* ps;
      int k = ps.a;
      int l = ps.f;
      ps.f = k;
    }
i.e. the field can be replaced by a function and the usage of it would not change.
Actually, those are things I like about C:

- It does not run code behind my back when I access a field.

- I see when a pointer is used, so the syntactic distinction between -> and . is important.

Being able to change this without changing these little things is far less important to me than being able to understand important properties of the code just by looking at it.

> it elevates a technical detail to be part of the syntax of the language

For me that's definitely a bad thing. It's the reason I don't like Rust syntax, they make you write everything out even if there is no possible way to misinterpret the code. And they never do things like Zig enum variant shorthand because syntax sugar is evil. Maybe they're just diabetic.

Rust Deref trait can do quite a bit behind the scenes. And there is (sort of) an enum shorthand, e.g. the following works. (It's more useful in match statements.)

  #[derive(Debug)]
  enum E { A, B, C }
  pub fn main() {
    use E::*;
    let x = B;
    println!("{:?}", x);
  }
Totally fair, I get that. I’m also not a hardened C dev, so maybe it would annoy me at some point. I gripe about Go’s assignment operators from time to time, but I like it on the net.

Not really Go in general, though. It’s mostly disappeared from my personal projects, in part because of their matching allergy to syntax sugar. I am incredibly tired of writing “if err != nil” blocks, and I don’t understand why there isn’t some kind of “if this function returns a non-nil error, return zero values and the error”. If only I could get something better to make a static binary. By which I mean “I like Clojure and refuse to learn SBCL because… reasons”

Rust offers this via de Deref trait