Git Product home page Git Product logo

Comments (4)

davidmorgan avatar davidmorgan commented on May 4, 2024

It'll need to be a factory rather than a constructor so the generated type can be used. The canonical way of doing what you suggest is:

factory Foo.of(String fieldOne, String fieldTwo) =>
new Foo((b) => b
..fieldOne = fieldOne
..fieldTwo = fieldTwo);

And indeed we see this pattern a lot where there is a small fixed number of fields.

If you're worried about performance, you could reference the internal constructor, but I don't want to encourage this:

factory Foo.of(String fieldOne, String fieldTwo) =>
new $Foo.(
fieldOne: fieldOne
fieldTwo: fieldTwo);

Instead I'll look at optimizing the cost of the builder-based version. It should be possible to get it pretty close to the direct-constructor version, and then that'll help all the other uses of built_value too.

from built_value.dart.

davidmorgan avatar davidmorgan commented on May 4, 2024

@kevmoo I looked at the performance a little; it's true that the direct version is faster, but both are so fast -- tens or hundreds of millions of instantiations per second -- that I don't think it's likely to make a difference in practice.

So I'd like to wait for a use case before optimizing.

Apart from that, does the factory example I gave do what you want?

from built_value.dart.

kevmoo avatar kevmoo commented on May 4, 2024

For "Heavy", I wasn't talking about perf. I was talking about usability. It's a crazy amount of typing to create a class w/ two trivial fields.

from built_value.dart.

davidmorgan avatar davidmorgan commented on May 4, 2024

Makes sense. Then, you can add a factory constructor as suggested above -- we often do this. Please let me know if there's anything missing.

from built_value.dart.

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.