cx-language / cx

C* is a hybrid low-level/high-level systems programming language focused on performance and productivity.
https://cx-language.github.io/
MIT License
130 stars 9 forks source link

pointer arithmetic #56

Closed PavelVozenilek closed 4 years ago

PavelVozenilek commented 4 years ago

I tried to modify example for pointers, to:

void main() {
    int i = 6;
    int* p = &i; 
    ++p; ++p; p++; // what would this do?
    println(p); 
}

I expected it not to compile, to crash, or print some nonsense, but got 9. It feels nonintuitive, that invisible dereference.

emlai commented 4 years ago

The current design is that every operation on a pointer operates on the pointed-to value. So they're more like C++ references in that way. I think this should be clarified in the documentation.

In fact, I wonder if the pointer type syntax should be changed to T& to make this behavior more intuitive to people familiar with C++ references. And maybe start calling them "references" as well.

Currently to do pointer arithmetic, so called "array pointers", T[*], need to be used. They're called that way, and the syntax is like that, because pointer arithmetic only makes sense on arrays. For example:

void main() {
    int i = 6;
    int[*] p = &i; 
    p++;
    println(p[-1]);  // prints 6
}
PavelVozenilek commented 4 years ago

Yes, this (reference) would be more intuitive.

emlai commented 4 years ago

Yes, references and T& could be better for Delta given their established behavior in C++. I'll think about it.

I also improved the pointer documentation now.

emlai commented 4 years ago

Consider changing pointer syntax to T&