tonynhan / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Should optional value types be Nullable<T>? #49

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I was playing about with protogen using the Person type from the Google 
website and wondered why a field declared with 

  optional int32 id = 2;

is not generated as a Nullable<int> in the corresponding c# class.

It seems to just get the default value (zero), which might not mean the 
same thing as 'missing'.

Original issue reported on code.google.com by nag...@gmail.com on 10 Mar 2009 at 6:52

GoogleCodeExporter commented 9 years ago
The problem with exposing it directly as Nullable<int> is that it doesn't have 
any 
way of passing the specified default value into the GetValueOrDefault; where-as 
the 
current implementation respects the default value if specified.

Fortunately, what you want is fully catered or as an option; add 
"-p:detectMissing" 
to the command line; this will give you a public bool idSpecified {...} that 
tells 
you whether it has an explicit value vs a default value.

Original comment by marc.gravell on 10 Mar 2009 at 10:27

GoogleCodeExporter commented 9 years ago

Original comment by marc.gravell on 26 Jun 2009 at 7:23

GoogleCodeExporter commented 9 years ago
I think that having optional fields translate to nullable is the way to go. 
It's certainly more convenient.

I would vote for:
- nullables for all optional
- field getter could have special logic for the case when default value is 
specified
- the codegen could output "isSpecified" for that special case

Original comment by filip.fr...@debesys.net on 30 Jul 2012 at 4:33