-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
C++-style initializer list #1602
Comments
I'd prefer hash_map!([i32, i32], 1 => 2, 3 => 4)
hash_map!([i32, i32],
1 => 2,
3 => 4) |
@KalitaAlexey what I suggest is that we introduce less sugaring, this way the syntax will be more powerful. For example, my proposal is not about pair, it just happened to involve This let v: Vec<i32> = <Vec<_>> { 1, 2 }; will be desugared to let v: Vec<i32> = [1, 2].drain_to_iter().collect(); |
@bombless When we introduce type-level integer generics we could make impl impl<N, T> From<[T; N]> for Vec<T> And use it in the following form: let v: Vec<i32> = [1, 2].into(); I am against this syntax because it adds complexity to parser, developers without any significant profit. |
@KalitaAlexey hash_map!([i32, i32],
1 => 2,
3 => 4) looks very ugly. I will try to implement my syntax. |
@bombless Uglier than: <HashMap<i32, i32>> { // wrapping type name with <> to indicate type like in UFCS
( 1, 2 ),
( 3, 4 ),
}; ? |
@KalitaAlexey As I mentioned above, initializer list is very powerful, otherwise C++ community won't accept it. |
@bombless I don't like C++. I am working as C++ programmer, but C++ is ugly. We shouldn't accept any feature from C++ just because it is from C++. Let's wait for another people opinions. |
Plus you don't add one macro for each type, that looks insane. |
@bombless Why not? Why does it look insane? |
@KalitaAlexey Macros exist only because type system is not powerful enough. |
Macros exist because it allows many things without extending syntax for each case. Rust will end like C++ with many-many features, but with ugly-ugly syntax. |
And about type system. impl<K, V, N> From<[(K, V); N]> for HashMap<K, V> And use: let m: HashMap<i32, i32> = [(1, 2), (3, 4)].into(); Without extra syntax. |
Like I said,
You see, let m: HashMap<i32, i32> = vec![(1, 2), (3, 4)].into_iter().collect(); already looks okay, so I'd suggest macro is off-topic here. |
I think @KalitaAlexey's integer-generics idea looks pretty nice, and it may be worth just waiting until then. My main issue with that approach is that it seems to involve pushing the entire array onto the stack and then moving each element into the map individually, which in extreme cases could lead to stack overflow if the optimizer isn't clever enough to figure that out. How do C++ initializer lists utilize stack space? |
@rphmeier Same. There is no way to utilize any more efficient. |
I see, type-level integer deserve a RFC. |
Absolutely agree about preferring type-level integers. I am strongly against additional Rust syntax without a very good motivation, and this motivation doesn't seem strong enough. |
I agree; this syntax is ugly. It should at least use type ascription instead of putting the type name in anglebrackets, and even then, I think we're better off with #542. |
👎 to extra syntax. There doesn't seem to be enough to pull it's weight. A macro would make sense, however. |
The point isn't the syntax, it is insertion at compile time, to improve runtime performance. |
The type system want to talk to you. |
One can't insert into a There's two reasons I would think special syntax might be faster than calling
|
Of course, but if the value is known at compile time it is very much possible to hash it at compile time. Even though, my points were mainly the two you listed afterwards. |
This is really a job for a macro (like |
Here is a valid macro: macro_rules! map {
($($key:expr => $val:expr),*) => (vec![$(($key, $val)),*].into_iter().collect())
} |
Just to be clear, |
(We should add variadics, basically) |
@ubsan I'll have to strongly disagree with "most hated" since the benefits of |
@Ixrec maybe only among my friends, then - it's so bad for two reasons. First of all, uniform initialization means something different than parenthesized initialization, and no standard library functions use uniform initialization. Second, it provides a const view of the data, which is awful for non-trivially-copyable types - basically, you're forced to copy every single value. Don't use uniform initialization for |
Closing in favor of #542. |
In C++ you can use an initializer to initialize a map, which looks like a map literal and feels nice.
playground
So I suggest we can do something like this:
which do similar thing as above C++ code.
We already have initializer list built-in so we don't need to tackle about the initializer side.
Let me explain how above Rust code work:
It is desugared to:
playground
The trick is that
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
is done already.To sum up, points are:
Now you might argue that if the desugared form is already okay why should we accept this fancy sugar.
I say this sugaring form is much more human-parsable and can make life easier.
The text was updated successfully, but these errors were encountered: