I quite like @cristicbz’s “struct literals” idea here.
There’s a nice symmetry here:
let i = 0i32;
let p = Point { x: 0, y: 0 };
// or
let i : i32 = 0;
let p : Point = { x: 0, y: 0 };
Inference would still need to unambiguously find the type, all the #[derive]
stuff goes in the usual place, but where something else made it clear, you don’t have to say what it is explicitly.
That gets the usage of the API to be exactly the same as proposed in the OP’s piston example. And I don’t think that it’s unreasonable for the API designer to make named structs for such things. Certainly I could imagine things like Position::Origin
to pass instead of { x: 0.0, y: 0.0 }
.
Default arguments would end up looking like Foo({ a: 32, .. FooArgs::default() })
, I guess? That’s not all that bad, actually, for a purely library solution…
(Hmm, I wonder whether ..
syntax would be sufficient to infer a type for a ‘struct literal’?)