News for hackers. Stuff that matters.
Re:Methods for Array Initialization in Rust (2018) by rwmj on Monday May 24, 2021 @08:59AM (#27263804)
What's the underlying reason for arrays <= 32 elements to be treated differently?
Re:Methods for Array Initialization in Rust (2018) by chrismorgan on Monday May 24, 2021 @09:01AM (#27263832)
It’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-... 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.

Re:Methods for Array Initialization in Rust (2018) by masklinn on Monday May 24, 2021 @10:02AM (#27264454)
> 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.

Re:Methods for Array Initialization in Rust (2018) by Diggsey on Monday May 24, 2021 @11:31AM (#27265527)
Why 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.

Re:Methods for Array Initialization in Rust (2018) by masklinn on Monday May 24, 2021 @02:15PM (#27267800)
Because Rust’s generics are patterned after Haskell’s (traits are dual to type classes), not C++’ templates.
Re:Methods for Array Initialization in Rust (2018) by Diggsey on Sunday May 30, 2021 @07:16PM (#27337930)
You'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.

Re:Methods for Array Initialization in Rust (2018) by swsieber on Monday May 24, 2021 @10:48AM (#27264984)
There 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.
Re:Methods for Array Initialization in Rust (2018) by chrismorgan on Monday May 24, 2021 @08:35PM (#27271406)
This 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.
Re:Methods for Array Initialization in Rust (2018) by rusbus on Monday May 24, 2021 @09:08AM (#27263902)
Traits 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.

Re:Methods for Array Initialization in Rust (2018) by MauranKilom on Monday May 24, 2021 @09:02AM (#27263843)
Really 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...
Re:Methods for Array Initialization in Rust (2018) by tialaramex on Monday May 24, 2021 @11:44AM (#27265697)
Yes. This is untidy but effective.

Here's what's left (this was greatly reduced by const generics already, as another poster has explained):

https://doc.rust-lang.org/src/core/array/mod.rs.html#383

That macro implements Default, but only for arrays of size 1 to 32. It doesn't make any sense to talk about a Default array of size 0, the array is empty, there's no "default" content at all.

You can see the same verbosity for places where in some other languages you'd be able to just say all these things just work with any number of elements in a tuple, and Rust has chosen "up to 12" instead because it cannot express that.

https://doc.rust-lang.org/src/core/tuple.rs.html#100

The top half of that file is explaining to Rust's macro language how to do various things for an N-tuple, and then the bottom half call that macro for the 1 through 12 sizes.

If you really need a 16-tuple (unlikely, but...) you could copy paste the code to make one with the same features. This isn't compiler support, so you could just do it yourself.

Re:Methods for Array Initialization in Rust (2018) by estebank on Monday May 24, 2021 @12:36PM (#27266440)
> If you really need a 16-tuple (unlikely, but...) you could copy paste the code to make one with the same features. This isn't compiler support, so you could just do it yourself.

There's one wrinkle in that strategy in that you might run afoul of the orphan rule[1][2] where you can't write:

    impl ForeignTrait for ForeignType {}
because if we did it would be brittle and changes to an external crate could make your unchanged crate stop compiling. So you're restricted to either implementing your own trait or any trait on a type you declared. To "get around this" you can use the new-type pattern[2]:

    struct MyArray<T>([T; 33]);
    impl ForeignTrait for ForeignType {}
[1]: https://github.com/Ixrec/rust-orphan-rules

[2]: https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re:Methods for Array Initialization in Rust (2018) by tialaramex on Monday May 24, 2021 @04:10PM (#27269116)
My error. Good point about the orphan rule. Because I was writing about Tuples what I was talking about is actually impossible in your own code I think, because tuples are inherently foreign. Even though the code in std looks tame, you are forbidden from doing this elsewhere. Even for a hypothetical local type MyType you are forbidden from attempting:

impl ForeignTrait for (MyType,u8)

The compiler will conclude that ForeignTrait is foreign, of course which is what we expected, but (MyType,u8) is also foreign even though MyType is local and so the orphan rule forbids this definition. Shame.

It doesn't make any sense to sidestep with new type because if you didn't want a tuple you could already define whatever type you wanted and this isn't an issue anyway, whereas if you do really want a tuple then a new type isn't a substitute.

Re:Methods for Array Initialization in Rust (2018) by hderms on Monday May 24, 2021 @09:18AM (#27263996)
It actually isn't really that unusual to have things like this in a statically typed language. Scala and Haskell have similar limitations with tuples
Re:Methods for Array Initialization in Rust (2018) by masklinn on Monday May 24, 2021 @10:03AM (#27264474)
Without const generics, traits had to be implemented on every arity. Even with macros, there has to be a limit somewhere in the number of impls you allow as the implementations are explicit and eager.

32 was the limit selected.

Re:Methods for Array Initialization in Rust (2018) by simias on Monday May 24, 2021 @10:07AM (#27264511)
It was normally implemented with macros to hide the boilerplate, but I agree that it wasn't very elegant. You can understand why many of us were very happy to finally get const generics...
Re:Methods for Array Initialization in Rust (2018) by nynx on Monday May 24, 2021 @09:07AM (#27263886)
It's not the case anymore.
Re:Methods for Array Initialization in Rust (2018) by MauranKilom on Monday May 24, 2021 @09:12AM (#27263939)
Ok, but why was it the case?
Re:Methods for Array Initialization in Rust (2018) by ekidd on Monday May 24, 2021 @09:26AM (#27264056)
Rust 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.

Re:Methods for Array Initialization in Rust (2018) by rwmj on Monday May 24, 2021 @02:25PM (#27267913)
Thanks, I think this is the clearest explanation in the thread.
Re:Methods for Array Initialization in Rust (2018) by kzrdude on Monday May 24, 2021 @01:04PM (#27266877)
Formal systems are rarely perfect :)
Re:Methods for Array Initialization in Rust (2018) by qw3rty01 on Monday May 24, 2021 @12:20PM (#27266221)
The 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`.