struct Foo(i32, i32);
struct Bar{sum: i32, product: i32}
But you can only e.g. return from functions an anonymous tuple, not an anonymous labelled struct fn can() -> (i32, i32)
fn cant() -> {sum: i32, product: i32} struct Foo(i32, i32);
struct Bar{sum: i32, product: i32}
But you can only e.g. return from functions an anonymous tuple, not an anonymous labelled struct fn can() -> (i32, i32)
fn cant() -> {sum: i32, product: i32} (int, int) can() => (1, 2);
({int sum, int product}) alsoCan() => (sum: 1, product: 2);
(int, {int remainder}) evenThis() => (1, remainder: 2);
The curly braces in the record type annotation distinguish the named fields from the positional ones. I don't love the syntax, but it's consistent with function parameter lists where the curly braces delimit the named parameters.Like how do you write the type of (1, {sum:2}) ? Is it different from (1 , sum :2)?
struct First(this: i8, that: i64)
struct Second(this: i8, that: i8)
struct Third(that: i64, this: i8)
struct Fourth(this: i8, that: i64)
struct Fifth(some: i8, other: i64)
You want First and Fourth as the same type, but Second and Third are different - how about Fifth?I see that this is different from Rust's existing product types, in which First and Fourth are always different types.
Second though, can you give me some examples where I'd want this? I can't say I have ever wished I had this, but that might be a different experience.
And their point is that the two boil down to the same thing, especially in a non-trivial program. If switching field positions around changes their semantics, tuples may well the most sensible choice. As for "what that random usize is supposed to represent" that's something that can be addressed with in-code documentation, which Rust has great support for.
If these aren't "random" usizes but FileSizes or ColumnNumbers or SocketTimeouts then say so and the confusion is eliminated.
I think the main dividing line here is whether you want to lean into strict typing or whether you prefer a more loose typing structure. The extremes of both (where, for instance, the length of an array is part of its type definition or there are not contractual guarantees about data) are both awful. I think the level of type strictness you desire as a product is probably best dictated by team and project size (which you'll note changes over the lifetime of the product) with a lack of typing making it much easier to prototype early code while extremely strict typing can serve as a strong code contract in a large codebase where no one person can still comprehend the entirety of it.
It's a constant push and pull of conflicting motivations.