Distinguish between integer and size #1659
Replies: 3 comments
-
Or if we have in place constructor, then we only have to specify the size when initialize the container. Or, all constructors receive pointers for the location for this kind of purpose. I know this is not new programmer friendly. I'm sorry. |
Beta Was this translation helpful? Give feedback.
-
I don't like the use of 's' as a literal for size. It's already used in C++ for strings (https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s) and I would prefer Carbon to do so as well to avoid confusion. May I suggest 'n' (num) instead if this would become a thing? Although I don't think this fixes the problem entirely: |
Beta Was this translation helpful? Give feedback.
-
Carbon doesn't use constructors, only factory functions, so I expect this difference to be made clear by the name of the factory function used. |
Beta Was this translation helpful? Give feedback.
-
In C++, there is such an inconsistency:
This leaves C++11's efforts to unify initialization syntax unfortunate, the second syntax cannot maintain the same semantics as the first.
In fact, for a collection class such as a vector, we want to express both how many members we need, as well as expressing that we need a member whose value is an integer.
Maybe we can make up for this regret in carbon.
Also, I don't want to have too much syntax on initialization, in order to be able to solve this problem in one initialization method, distinguish between integer and size, which can be well handled.
We use integers to represent a lot of things, which is fine, but not good. An integer cannot represent any additional meaning, it is just a number. For example, we could use an integer to represent the week, but this leaves a hidden danger. The week does not need such a large range, and an enumeration or a custom class is a better choice.
Size doesn't have range issues, but it has additional semantics, in this case it expresses the number of container members, which cannot be expressed by integers, even though size can be modeled on integers.
I wish we can have syntax like this(c++ syntax) :
What syntax we should have in carbon need consider.
Beta Was this translation helpful? Give feedback.
All reactions