Scalapb: Add an option for removing default parameter value in generated codes.

Created on 12 Aug 2019  路  4Comments  路  Source: scalapb/ScalaPB

Currently, there is default value for optional field in codes generated by scalapb.
For example, there is the proto file message foo { int32 x = 1; } written in proto3.
Generated code by scalapb is like: final case class Foo(x: _root_.scala.Int = 0).
The problem is that a programmer can omit putting x when encoding data to proto because there is no compile error due to the default value (0).
So, removing default value in generated codes by scalapb is good for preventing programmer's mistake.
So, I suggest adding new option for removing default parameter value.

Most helpful comment

Released ScalaPB 0.9.1, with no_default_value_in_constructor file-level option.

All 4 comments

Thanks for making this feature request and sending a PR. Sorry for the delay in getting back to you. I wanted to take the time to consider this change before getting back to you. While I understand the motivation behind this issue, I am hesitant to add an option to inhibit default values. My main concern is that features like this increase the complexity of the generator and as a result come with a maintenance cost, while only being useful for a small subset of users. It is also hard to remove them if they turn out to cause problems, since removing a feature like this significantly breaks code. Also, the behavior resulting from this feature is not aligned with other protobuf implementations.

For the time being, I am closing this issue. I am willing to reconsider if this turns out to be something people want, so the complexity and maintenance costs are warranted.

Thanks again for proposing this, and I appreciate the time you took to write a PR.

@thesamet Thanks for your kind comment.
However, IMHO, I think that this option is important and very helpful. I don't think that it is only for a few users.
What I said is not that removing default value of optional field in protobuf.
What I said is preventing a user from omitting putting a field by mistake when constructing a protobuf object.
In many cases, a user want to put not default value but specific value to a field when constructing a protobuf object. But currently, a user can make a mistake so easily because of default value in constructor.
If there is no default value in constructor, a user don't make that kind of mistake. And even if a user want to put default value to field, he/she can do easily through putting default value explicitly.

Really my team made that mistakes frequently since introduced scalapb long time ago. Scala is the strongly typed language and many scala developers rely on compile time type system. But, the current behavior (default value in constructor) is contrasting to such things.
So I think the option I suggested can be useful for many scala developers.
I'm sorry, but could you consider one more time, please?

The use case and motivations are clear. I'm more comfortable adding it without a generator parameter (proto options only). Let's consider this experimental, we may remove it if there are some unintended consequences.

Released ScalaPB 0.9.1, with no_default_value_in_constructor file-level option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nykolaslima picture nykolaslima  路  9Comments

danvk picture danvk  路  8Comments

ghost picture ghost  路  3Comments

dhuadaar picture dhuadaar  路  13Comments

zhangandyx picture zhangandyx  路  5Comments