Why change null to another value?
pcode-xyz opened this issue · 4 comments
Sorry, my English is bad.
in file PhpGenerator.php line 711.
'return $value === null ? ' . $returnCast . '$value : $value;'
Why change null to another value?
Is this a bug?
I think this may be more reasonable.
'return $value !== null ? ' . $returnCast . '$value : $value;'
I'm not able to answer your question why it's done this way (this code was written years ago). I can say though, how I would approach it today. The most reasonable approach boils down to:
'return $value;'
Why? Firstly $value
has proper type if a value is set therefore no need to type casting. Secondly null
directly informs you that the value was not set.
Nevertheless changing this would break the established contract. It would need to be introduced with a way to preserve backward-compatibility for those who already rely on it.
I'm not able to answer your question why it's done this way (this code was written years ago). I can say though, how I would approach it today. The most reasonable approach boils down to:
'return $value;'
Why? Firstly
$value
has proper type if a value is set therefore no need to type casting. Secondlynull
directly informs you that the value was not set.Nevertheless changing this would break the established contract. It would need to be introduced with a way to preserve backward-compatibility for those who already rely on it.
Thank you
The scene I am encountering is the need to get the original value of the data.
If this value is null, it means it is not set.
Used to distinguish zero, an empty strings, etc.
So I found this line of code would prevent me from implementing such a feature.
Waiting for your fixed code
And, thanks again
It has turned out I should be able to remember why null values are cast 😄 Here you can find a discussion that lead to this change. The bottom line is it works with accordance with official Google's guideline and hence won't be changed.
@bingxuecandong in the latest release I have added generation of has methods which you can use to check whether a field is set.