Giter Club home page Giter Club logo

Comments (9)

tbroyer avatar tbroyer commented on June 9, 2024 2

@ooxi Have a look at the javadoc, there's a new @AutoValue.Builder annotation.

from auto.

eamonnmcmanus avatar eamonnmcmanus commented on June 9, 2024

This is mentioned in the documentation:

Many users have asked for AutoValue to generate a builder class. We explored this idea deeply. It is much more complex than it seems, especially because field values often need validation. We also feel that, unlike the simple value objects themselves, there is a lot of natural variation in how builders for different types should be written, and it would be a disservice for us to start coercing everything into the same mold.

As a side-note, your sketch can't work because MyValue doesn't have a builder() method, and standard annotation processors can't insert methods into existing classes. Probably if we were to support builders we would generate a public MyValueBuilder class, and you would write

MyValue value = new MyValueBuilder()
    .withFirst("hello")
    .withSecond("world")
.build()

That is in fact essentially what I did in the JavaFX API with another annotation processor. But the objects being built there had public constructors whose parameters defined which properties were required and which could perform any necessary validation. The fit with AutoValue is less good because we don't currently have a way to specify that some properties are optional, and as the text above mentions we also don't have a way to validate them. There's also the question of what the property methods in the builder should be called. withFirst(x)? setFirst(x)? first(x)?

The best approach I can see would be to allow @AutoValue or another annotation to apply to a static factory method (like create in the examples), or perhaps a constructor. The parameters of the method or constructor would then have to correspond to the class's properties, and it could contain validation code. We would probably want another annotation that applies either to the abstract property methods or to the method parameters, specifying that the corresponding property doesn't have to be supplied to the builder, and perhaps also what the default value is then. But as you can see builder generation is already quite a bit more complicated than the simple idea behind @AutoValue classes.

from auto.

ooxi avatar ooxi commented on June 9, 2024

Yes you are right that a builder class is generally much more complicated than a value class. My incentive comes from the fact that we had a couple of constructor arguments of the same type and developers constantly did not remember the correct order.

Since you clearly considered adding a builder and for various reasons preferred not to I'm closing this issue.

from auto.

elucash avatar elucash commented on June 9, 2024

If you are interested in builders you might consider to look at the design of builder generation in Immutables.org project, see http://immutables.github.io/immutable.html#toc6.
You can compare how complex or involving could be to implement this in AutoValue.

from auto.

felipecsl avatar felipecsl commented on June 9, 2024

Looks like this has been just added a few days ago #175

from auto.

ooxi avatar ooxi commented on June 9, 2024

@felipecsl Looks great! Is there any documentation on how to use this?

from auto.

eamonnmcmanus avatar eamonnmcmanus commented on June 9, 2024

Yes, there are a few final improvements and a documentation change in the pipeline and they should show up at the next sync from Google's internal branch. Feel free to experiment; the javadoc should be enough to get the gist.

from auto.

ooxi avatar ooxi commented on June 9, 2024

👍 great improvement :)

from auto.

h908714124 avatar h908714124 commented on June 9, 2024

AutoBuilder? Yes! https://github.com/h908714124/auto-builder

from auto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.