Comments (4)
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.
@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.
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.
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)
- `JsonObject` factory accepts null, but will throw when null is passed HOT 1
- `enum_source_class.dart` contains faulty mixin code in dart 3 HOT 7
- `JsonObject` and sub classes could have a const constructor
- Auto add builder factories for nested built_collection classes HOT 2
- Question: Possible to serialize & deserialize a field of type Map<String, dynamic> ? HOT 14
- Getting enum by wire name HOT 2
- Classes not being generated with new Dart 3 classes modifiers HOT 2
- Support analyzer v6 HOT 1
- Int32 serializers HOT 1
- Allow record serialization (or bypass the error when generating) HOT 10
- Bug: The key of a List is included as an element of said list. HOT 3
- High Memory Usage of built_value_generator to due memoization + keeping all SerializerSourceClass objects in memory HOT 6
- Allow `$` in enum class names similar to other built_value classes
- BuiltJsonSerializers throws an Error on deserialization exception but it should? HOT 4
- Nested builder factories not added automatically in serializers.g.dart HOT 2
- Record list serialization unexpectedly converted to a map by StandardJsonPlugin. HOT 5
- Classes not being generated with class-modifiers HOT 3
- [Bug] Code Generation Attempt Unintendedly Triggered HOT 3
- optimize building generated files HOT 6
- Allow inherrited BuiltValueHooks HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from built_value.dart.