prost icon indicating copy to clipboard operation
prost copied to clipboard

Structs should have option to generate with default values rather than Option

Open jollygreenlaser opened this issue 2 years ago • 3 comments

https://developers.google.com/protocol-buffers/docs/proto3#default

Take a simple example

message Coords {
    int32 x = 1;
    int32 y = 2;
}

message Player {
  string uuid = 1;
  Coords coords = 2;
}

This will generate with Player.coords being an Option. This is reasonable, compared to the has_coords that most other languages have. However, most other languages would still have the default value in for coords. If you know that coords is there and valid, you don't need to mess about.

Option is zero cost in running but can be significant clutter in code if not necessary. I understand that for many people, this is how they want their proto to generate. It would be nice to have the option to remove option and just put the default value if not present.

jollygreenlaser avatar Nov 25 '22 04:11 jollygreenlaser

I agree this would be an awesome usability win instead of having to .context("Field unexpectedly asbent")? on every field.

banool avatar Jan 03 '23 17:01 banool

Just ran into this and was very confused why a bunch of fields are "optional"

I don't feel like Option is ever the right choice for default values, if the optimization is wanted it should be another type

ForsakenHarmony avatar Aug 05 '23 06:08 ForsakenHarmony

Tonic view: I wonder if an option would be to expand on .extern_path() and similar APIs on the builder in tonic. Something like .use_default("name of type") to make it use Default::default rather than wrapping in Option.

mitsuhiko avatar Apr 17 '24 06:04 mitsuhiko