New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Overload square bracket for both array and vector initialization #364
Comments
With all due respect, I'm not quite for this proposal, since this may cause 2 consistency problems. A) Inferred data type differs when the initialization value appears as literal or variable binding:
B)
In my opinion, In fact, the real difficulty is how to shorten the initialization syntax of
The prelude mechanism of Rust language may also be introduced. |
@shamiao Thanks for sharing your thoughts. Sorry my original example was confusing, the type annotation is not required if vector is the desired type:
A) Since the vector type is favored in unclear context, we'd have
B) We will also change the way how vectors and fixed arrays are transformed to string to make things consistent. C)
FixedArray[T] can still use the syntax |
@Yu-zh Thank you very much. I basically agree with the core idea of edited proposal, with some minor different details: (some ideas have already mentioned in MoonBit 用户交流群 WeCom group ) A) Remove the concept of array from primitive types. All linear collections should be provided by B) Rename Actually, naming grow-able array 'vector' is a historical mistake from C-like low-level languages. The name vector is abandoned by many recent GC programming languages, we also have a chance to adapt now. C) Thus, D) Consider |
Motivation
The Vec datatype is used frequently but for now we have to write
@vec.Vec::[1, 2, 3]
to create a vector with init elements. It's even worse when we want to write nested vectors like:@vec.Vec::[@vec.Vec::[1, 2, 3], @vec.Vec::[4, 5, 6]]
.By contrast, Array is used less often but has a more handy syntax. So we could overload the
[1, 2, 3]
syntax so that it creates either an array or a vector according to its context. Vector is favored when the context is unclear. For example:This is also consistent with some other language features in Moonbit. In particular, we are using
[ a, b, .. ]
for pattern matching on both Vector and Array. Also, the data constructors are disambiguated by the typing information from the context.Comments and suggestions are appreciated.
Edit: vector type annotation is not required in the example
The text was updated successfully, but these errors were encountered: