3 ms·
> Why do we need both . and -> ? The compiler is always able to know which one makes sense from the type of the variable anyway. I've often wondered this mysel
by Boxxed 7y ago
> Why do we need both . and -> ? The compiler is always able to know which one makes sense from the type of the variable anyway.
I've often wondered this myself. The best I can come up with is that the underlying code generation includes an additional dereferencing step with ->, so having both . and -> makes the compiler a little more transparent.
Of course, in C++ you really need both because overloading -> is nice for e.g. objects that want to look like pointers.
- paulddraper 7y agoMy guess is they liked the explicitness of it.
- jkoudys 7y agoThis was solved in rust by approaching it from the other side. You need to be explicit everywhere that a value is "borrowed", so while you never need a `->`, you'll still know it's dereferencing in the more natural place - before the variable, not as an implied step after.