Why are arrays special? #1511
Replies: 3 comments 3 replies
-
I think the special syntax is just because arrays are common. It's likely to be syntactic sugar for a library implementation (similar to what you're writing, although I don't think we've decided on names), not builtin. Making things syntactic sugar instead of builtins is generally a goal; numeric types are an another example where the language is essentially providing shorthand, such as |
Beta Was this translation helpful? Give feedback.
-
Right, so this is what I disagree with. Outside of very low level or memory constrained code, you will (in my experience) almost always be using a variable sized container (std::vector) not a fixed size one.
The i32 shorthand for a type doesn't break the expectations for what a type looks like. I'm not quite sure how I would write the type of an array in a generic context for example, because it looks so different from all other types. |
Beta Was this translation helpful? Give feedback.
-
One thing to bear in mind is that the array support in Explorer is a preliminary sketch; there isn't yet an approved design for arrays. The eventual proposal for arrays will need to address these questions. With that said, part of the motivation is that we need a concise syntax to support the upcoming design proposal for variadics, which relies fairly heavily on unsized arrays of types. For that use case, array types will probably need to be given special treatment by the core language, and it's important for them to be pretty concise. |
Beta Was this translation helpful? Give feedback.
-
There is a special syntax and implicit conversion for arrays, that at the same time feels both awkward and unfamiliar to me:
In addition, arrays have special rules where they implicitly convert from tuples, and don't need their size specified in the type.
My question is: why are we special casing arrays? In general, most use cases shouldn't be using raw arrays but some higher level container, and the presence of special syntax just for arrays feels like an unnecessary carryover from C.
I think one could build the interface (not the implementation necessarily) for arrays on generics and variadics by doing (as stated in the docs https://github.com/carbon-language/carbon-lang/tree/trunk/docs/design#checked-and-template-parameters)
All the names are strawman syntax, but I think its worth asking whether arrays need to have a special interface different from other containers.
Beta Was this translation helpful? Give feedback.
All reactions