2
AlgoRythm
24h

Can anyone make a good case for the dereference-property operator being "->"

What the fuck, those keys are nowhere near where I rest my fingers while typing.

Not only that, but it's a sequence of two characters. For what reason!?!?!?

In fact, can anyone tell me what the reason is that we have a specific operator for this operation?! Why not just use the period for everything? If the left hand side is a pointer, the period can dereference and THEN resolve the scope. Otherwise, just resolve the scope to the property. Since resolving the scope on a pointer directly does nothing (IE, if you were to write "pointer.property" in current c/c++/etc), this means the operation is unambiguous and doesn't need to be two operators.

FUCK.

Comments
  • 3
    Huh?
    IIRC, that’s a short cut for (*p).member
    So you can't just use the dot, it would try to access the member on the pointer directly.

    It’s not like C# where you have value types and reference types. So you need to have something else than a dot.

    But I agree, they should have come up with something that is just one character.
    Maybe @, but that would probably look like shit, just like objective c
  • 1
    For the keys issue, it's also why I hate `:` in kotlin. I tolerate it in python and typescript since they are modifications of the original language, but it makes me ewwww whenever a lang tries to use it.

    Probably for making the compiler impl easier? I'm pretty sure it's comes from the 90s a modern lang would probably simplify it indeed.
  • 2
    They're very different operations though. If they used the same operator it would be confusing.
  • 5
    Golang does that. When you're working with a pointer it dereferences via dot and if It's a value copy (which is the default behavior) it accesses the value member.

    So It's definitely doable to use the dot for everything. I think the reason why C's approach might be better is that it's explicit.

    Explicit is always better (as in, less error prone) than implicit, so I get it.

    It's not a big issue in Go eitherway.
  • 1
    @Hazarth Go has raw pointers? This language is freaking weird!
  • 0
    @BordedDev what’s your issue with : ?
    Imo it’s much better than Java's "extends" keyword 😂
  • 2
    @Hazarth I honestly feel like the ambiguity is caused by the fact that the c language separated them into two different operations in the first place! If c used the period for everything, then another language later on introduced the ->, we would think it is too verbose and dumb! They fundamentally do the same thing, just a dereference is in the air somewhere.
  • 1
    De-referencing is noted with the * symbol, you use -> because you are working with the heap memory (dynamic alloc, objects must be destroyed explicitly etc...), dot symbol (.) is for stack memory (auto managed by the compiler), etc...
  • 2
    @afaIk I don’t think that’s true.
    You can have a pointer pointing at a value created on the stack and still use -> even though it’s not on the heap.

    struct A { int foo };

    A a;
    A *p = &a;
    p->foo;
  • 0
    @Lensflare specifically this (colon is an annoying place on the keyboard):

    let thisIsRetard: BecauseIWantToKnowTheType = ofThis.thing
  • 1
    @Lensflare It does, though it usually tries to hide the specifics of it from you. It's mostly trying to do the C# thing where you either pass by value (default) or pass by reference (explicit in C# you'd use the "ref" keyword, in Go you just use the "*" symbol, so feels similar to pointers in C)

    however you can't do pointer arithmetic on those references unless you go out of your way to specifically cast it to an unsafe pointer type. Infact there's an entire package called "unsafe" dedicated to very low level access.

    Honestly, I've been working on and off with Go for about 2 years now? And I like many things it does. There's still a bunch of things I'd like done differently, and it's a very verbose language due to it's error passing style, but for many things it's just very efficient and easy to work with :D
Add Comment