class pointer sugar #1545
Replies: 5 comments 35 replies
-
here is a simple example (inspired by that video): You start programming for your new side project to forget about 100 previous. class MineField {
var width: u64;
var height: u64;
} then you create a function to calculate area of that field: fn calculateMineArea(mf: MineField) -> u64 {
// imaging there is many of mf.X usage
return mf.width * mf.height;
} after long programming session, you see you need a huge additional data to keep in
So it's obvious to you that, now it's inefficient to copy whole object to just calculate the area, you decide to use pointer instead: fn calculateMineArea(mf: MineField*) -> u64 {
// ...
} Now you realize that you need to replace all those Another similar situation is you need to convert a pointer-arg to a value-arg (for example for better performance - no pointers , ...), now you need to convert all Imagine in a large code bases, with many functions to convert, how it's inefficient this process. |
Beta Was this translation helpful? Give feedback.
-
I'm thinking I agree with this. Hasn't the general trend in C++ been towards value types and hiding pointers (and smart pointers) as implementation details, especially in Boost and the Standard library? The concept-model idiom has been around for over a decade and more recently the C++ metaclasses proposal was introduced to allow for the autogeneration of polymorphic type-erased facades, with pointer semantics and lifetime details abstracted away (similar to Rust's Maybe I misunderstand, but the trajectory that C++ is taking seems to be odds with Carbon's choice of syntax here. From reading the available documentation and proposals for Carbon, it looks like there are plans to enable type erasure, metaprogramming and generation of type-erased facets from archetypes. But is there still a way that Carbon can allow for implicit pointer semantics with |
Beta Was this translation helpful? Give feedback.
-
I agree that there's a real problem here, but I'm not sure if this sort of auto-dereferencing is a viable way of solving it. The problem is that Carbon, like C++, will probably have a lot of smart pointer types like Of course, we could come up with rules for disambiguating among those expressions, but there would still be a risk that the meaning the author intended (or the meaning the reader understands) is different from the meaning that the disambiguation rules choose, leading to unpleasant surprises (anecdotally, I've heard that this can happen in Rust). It also creates problems for software evolution, because it means that adding a method |
Beta Was this translation helpful? Give feedback.
-
The original reasons for the existence of -> don't even exist anymore. But it has since morphed into something completely different. I think the most important difference between . and -> in C++ is the fact that the -> operator can be overriden and the . operator can not. Which brings us to smart pointers, that override the -> operator. This means that there is no syntactic difference between accessing a pointer and a smart pointer implementation:
and
This means that if you change from a regular pointer to a smart pointer or vice versa, no other code changes are required. Overriding the . operator sounds like it will be more trouble than its worth. You know what . does, and you also know that -> could be overridden. If only . operator exists and can be overridden, you can't trust anything you do with an object without checking if it overrides . operator. Not to mention the fallout if someone adds a . operator override in the future to an existing object.
and
Both the smart ptr template and X have a member function get. So does the following code access the get of X or ptr if you only have the . operator?
You could say that you need to dereference the object to make a distinction:
But this leads to pretty ugly code in some case: |
Beta Was this translation helpful? Give feedback.
-
Probably is because operator-> does concatenation while "." does plain offset. For the cases with only 1 layer, dot and -> result the same. If you prefer dot than ->, it's possible to replace it in such case. But with more layers, -> is still a thing. My personally prefer ".." rather than ->. Double dots and the classic single dot are similar and easier to type. Readability should not be an issue. The only trouble is that, when a->->b, double dots works like, a....b, probably a space is needed in the mid, like, a.. ..b. But since we can encourage people NOT to do this, just like very very rarely we see any ->-> in c++. Short version: |
Beta Was this translation helpful? Give feedback.
-
if we have a class, like:
class A { var x: i64; }
and a variable from it, like:
let a: A = ...;
.then we can access
x
via dot, like:a.x = 5;
;if we make a pointer from that, like:
let b: A* = &a;
there is two way to access
x
,first via dereference+dot, like:
(*b).x
second via syntax sugar, like:
b->x
What is the downside of implicit dereferencing in this scope? like:
b.x
Why can't the compiler automatically detect
<ptrOfX>.<fieldOfX>
and treat it like a normal<objOfX>.<fieldOfX>
?Is there any benefits in explicit dereferencing ?
There is a good youtube video from Tsoding, showing how easy it is to add this feature to TinyC, and how convenience it is to program without
->
torture.Beta Was this translation helpful? Give feedback.
All reactions