4 ms·
What's the underlying reason for arrays <= 32 elements to be treated differently?
by rwmj 5y ago
What's the underlying reason for arrays <= 32 elements to be treated differently?
- chrismorgan 5y agoIt’s a historical limitation, with the power of lifting it only very recently stabilised (in the last couple of releases). Most things that were limited to 32 when this article was written aren’t any more, though the Default trait implementation still is. The key term to search for and read up on is const generics. This style of limitation is still present on tuples, which have implementations for common traits for up to 12-tuples. (See the bunch of implementations around https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#impl-PartialEq%3C(A%2C%20B%2C%20C%2C%20D%2C%20E%2C%20F%2C%20G%2C%20H%2C%20I%2C%20J%2C%20K%2C%20L)%3E https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#impl-... as an example, comparing it with the single “PartialEq<[B; N]> for [A; N]” implementation for all arrays!) This limitation will probably be sorted out eventually, but it takes time to settle on a design that satisfies everyone and is implementable.
- masklinn 5y ago> This limitation will probably be sorted out eventually, but it takes time to settle on a design that satisfies everyone and is implementable. That sounds unlikely, it’s would require not just const generics but some sort of variable arity generics. If Haskell doesn’t have it, I don’t really see Rust getting it.
- swsieber 5y agoThere have been various variable arity generic discussions on the internals forum (internals.rust-lang.org), but I think it's a significantly lower priority issue than the other stuff in flight. I think we might get it in a few years.
- chrismorgan 5y agoThis is my position, too. I think everyone agrees that we want to get generic tuples, but there are other higher priority issues still, so it’s mostly more a backburner project worth thinking about when designing things, and slowly working towards as good a design as we can come up with which fits in with the rest of the language (including future possibilities) as well as possible. Even in a vacuum it’s not trivial to design something both satisfactory and implementable, and when you add other language features, corners the language has been designed into, and corners you want to avoid painting the language into, it gets much harder still.
- Diggsey 5y agoWhy not? Rust isn't limited to what Haskell does, and a lot of it's features are driven by a desire for parity with C++, which does have variadic generics. Other languages in the same domain (like D) also have variadic generics.
- masklinn 5y agoBecause Rust’s generics are patterned after Haskell’s (traits are dual to type classes), not C++’ templates.
- Diggsey 5y agoYou're conflating traits with generics. Generics exist without traits, and in Rust they are more closely aligned with C++ templates than anything else, since they are implemented via monomorphisation, whereas in Haskell they are not. Regardless, Rust is not "patterned" after a single other language. It's influenced by many langauges, so it makes no sense to say that one feature is just copied straight from a specific other language.
- deleted 5y ago[deleted]
- MauranKilom 5y agoReally stuck out to me too. Does that mean that somewhere in there there are 32 different "overloads" (or whatever the right term is) of this functionality for 1 to 32 elements? It feels so out of place...
- nynx 5y agoIt's not the case anymore.
- MauranKilom 5y agoOk, but why was it the case?
- ekidd 5y agoRust has two low-level "array" types: - &[Elem]: A "slice" of elements, represented as a pointer and a length. - [Elem; N]. A fixed-size array where the number of elements is known at compile time. This might be used for 2D or 3D coordinates, for example. Array slices have always supported arbitrary lengths. But arrays with fixed compile-time sizes are represented as entirely different types: it's an error to pass a [i32; 2] to a function that wants an [i32; 3]. The size of the fixed-size array is part of the type. And until very recently, Rust's type system didn't support defining methods on [Elem; N] that were generic over N. This is a feature that tends to be added later in many languages that support generics, not just Rust.
- rwmj 5y agoThanks, I think this is the clearest explanation in the thread.
- hderms 5y agoIt actually isn't really that unusual to have things like this in a statically typed language. Scala and Haskell have similar limitations with tuples
- 5y ago
- rusbus 5y agoTraits are applied to types and because the length of the array is part of the type, different length arrays are different types. It's only with the very recent launch of const generics (allowing constant values like numbers as generic type parameters) that it became possible to abstract over things like array length. IIRC there is some stickiness about the default trait that is still blocking a const generic array initializer.
- qw3rty01 5y agoThe case in the article is for when `T: Default`. The reason this limitation exists is because it was decided that an array length of 0 should impl `Default` even if `T: !Default`, so `impl<T: Default, N: usize> Default for [T; N]` can't be used. This may be possible in the future if specialization is stabilized, but for now we're stuck with that limit for `Default`.