Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Value classes for primitive single-element message? #4

Closed
seacourtaw opened this issue Dec 19, 2014 · 3 comments
Closed

Value classes for primitive single-element message? #4

seacourtaw opened this issue Dec 19, 2014 · 3 comments

Comments

@seacourtaw
Copy link

Would it be possible for messages with one required field of value type to extend AnyVal? I am presuming not, but it's not completely clear from a cursory scan of the code.

@nadavsr
Copy link
Contributor

nadavsr commented Dec 19, 2014

Currently this is not the case. I believe we can do that and it would be relatively easy to add. Any reason this shouldn't be applied to non-primitive single-element messages?

@seacourtaw
Copy link
Author

Hmm. Good question. In the particular use case I envision, I have a set of
classes representing physical units. At the moment they are value classes
(not from protos) and I have a set of methods that allow them to be
combined appropriately (e.g Speed * Time = Distance). I would like to be
able to serialise them (and the class hieararchy they live in) to proto.
ScalaPB looks great - but in the current formulation if I define a set of
single-element messages (and use typeclasses to extend with methods) I lose
the value class optimisation.

It could also be useful for non-primitive single-element messages. Either
way it'd be great to have.

On Fri Dec 19 2014 at 6:41:21 PM Nadav S. Samet notifications@github.com
wrote:

Currently this is not the case. I believe we can do that and it would be
relatively easy to add. Any reason this shouldn't be applied to
non-primitive single-element messages?


Reply to this email directly or view it on GitHub
#4 (comment).

@nadavsr
Copy link
Contributor

nadavsr commented Jan 1, 2015

I looked into this and the change isn't as straightforward as I hoped. Given that required fields are going to be dropped in the upcoming proto3 syntax, there is a little value in implementing this. I'm interested to know if the AnyVal optimisation makes a measurable performance difference in your use case and then we can figure out if it's worth the investment.

In the mean time, I suggest that you use your own case classes that extend AnyVal during the life of your program, and map them to/from protobufs when you need to serialize/deserialize.

@nadavsr nadavsr closed this as completed Jan 1, 2015
thesamet added a commit that referenced this issue May 29, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants