Comments (6)
Hi Filip,
Good questions.
The main thing you can do to reduce the boilerplate is to stop writing the builder yourself. If you do that, a default/empty one is generated for you. I notice you're initializing some values to defaults -- but you could do that in your "initialized" constructor instead. Would that work?
The other thing I notice in your code is that your builder holds a "BuiltList". You could instead have it be "ListBuilder"; this is what it will default to if you don't define your own builder. This will save you a bit of typing:
factory FightSituation.initialized( Iterable<int> playerTeamIds, Iterable<int> enemyTeamIds) => new FightSituation((b) => b ..playerTeamIds.replace(playerTeamIds) ..enemyTeamIds.replace(enemyTeamIds));
The idea of built_value is absolutely to minimize the amount of boilerplate you end up writing, so we can dig into this until we hit something that looks optimal :)
from built_value.dart.
Thanks, this is extremely helpful!
In the end, it's easier for me to implement
Situation instead of extending it. It doesn't let me share some code but at least it works.
A feature request would be to have a section in the readme (or an example in examples/
) with a concrete tutorial on how to solve inheritance vs BuiltValue. I think that even if most of this tutorial would be copy-pasted from your reply above, it would be helpful.
Unless I'm mistaken, the idea of having many classes that inherit from A, which itself is a BuiltValue, will be quite a common use case.
Btw, I'm not sure if that's unsupported or a bug, but if I have:
abstract class Situation<V extends Built<V, B>, B extends Builder<V, B>>
implements Built<V, B> {
// ... common interace ...
}
and then I want a built value like this:
abstract class FightSituation
implements Situation<FightSituation, FightSituationBuilder> {
// ... concrete implementation ...
}
then the source generation fails with this:
// **************************************************************************
// Generator: BuiltValueGenerator
// Target: abstract class FightSituation
// **************************************************************************
// Error: Invalid argument(s) (builtParameters): Must not be null
I have to explicitly implement BuiltValue for this to work:
abstract class FightSituation
implements
Situation<FightSituation, FightSituationBuilder>,
Built<FightSituation, FightSituationBuilder> {
// ...
}
Should I file this as a bug?
I'm closing this one because my issue has been solved 👍
from built_value.dart.
I spoke too soon. In the end I dropped the requirement that the Situation
class must implement BuiltValue, and I go by convention. It was just too much hassle.
from built_value.dart.
FYI with the latest version relaxing what you can do with the factory, you can now set defaults in the default factory, e.g.
factory Dialog([updates(DialogBuilder b)]) =>
new _$Dialog((b) => b
..inProgress = false
..update(updates));
from built_value.dart.
Is the "relaxing what you can do with the factory" documented? I think I can grasp the constraints from your example but it would be better to be sure.
I still use the three constructor everywhere (factory FightSituation.initialized(...)
, factory FightSituation([...]) = _$FightSituation
and FightSituation._();
) and that sucks.
from built_value.dart.
The constraint is that you have to mention the generated type. i.e. anything with _$FightSituation in it is allowed.
This is pretty arbitrary -- if that's too tight we can loosen it further.
from built_value.dart.
Related Issues (20)
- `Function` types with named params are not handled correctly
- Still active? HOT 2
- Generation breaks on top level field with type containing `Never` HOT 1
- Serialization does not serialize derived values HOT 2
- Generic types & serializers HOT 8
- Feature request: SerializersBuilder addAllBuilderFactories HOT 2
- Support milliseconds with ISO8601DurationSerializer HOT 3
- escaping invalid field names with a '$' breaks the generator on custom types HOT 1
- Record field type breaks generator HOT 1
- `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
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.