You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by Bryan Duxbury <br...@rapleaf.com> on 2008/12/18 20:23:13 UTC

Make default values for optional fields illegal?

I was thinking a little bit about the potentially hairy situation of  
trying to deserialize a struct that has optional fields with default  
values. The problem that could happen is that the optional field is  
omitted from the serialized data, but once you're done reading, you  
find your struct has a value for that field. I guess some people  
might appreciate this situation, but personally, I think it would  
lead to weird inconsistencies.

However, if we made the compiler throw errors when you try to assign  
a default value to an optional field, then this problem could never  
happen. Required fields would have to be present from the get go or  
you'd get serialization errors, so there's no risk of running into  
this problem.

What does everyone think about this option?

-Bryan

RE: Make default values for optional fields illegal?

Posted by Mark Slee <ms...@facebook.com>.
I think what should happen in this case is that we should assign the default value to the field, but leave the __isset marker as false. This way if someone wishes to explicitly check for its presence, the isset will properly reflect what was in the serialized data, but the default value can still be freely used.

Do you think that'll still cause weird situations?

-----Original Message-----
From: Bryan Duxbury [mailto:bryan@rapleaf.com] 
Sent: Thursday, December 18, 2008 11:23 AM
To: thrift-dev@incubator.apache.org
Subject: Make default values for optional fields illegal?

I was thinking a little bit about the potentially hairy situation of trying to deserialize a struct that has optional fields with default values. The problem that could happen is that the optional field is omitted from the serialized data, but once you're done reading, you find your struct has a value for that field. I guess some people might appreciate this situation, but personally, I think it would lead to weird inconsistencies.

However, if we made the compiler throw errors when you try to assign a default value to an optional field, then this problem could never happen. Required fields would have to be present from the get go or you'd get serialization errors, so there's no risk of running into this problem.

What does everyone think about this option?

-Bryan