Last active
November 7, 2019 10:53
-
-
Save lrhn/7054b676966484b3fd457be158332dd2 to your computer and use it in GitHub Desktop.
Example of extension of built values list-builder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Built<V extends Built<V, B>, B extends Builder<V, B>> { | |
V rebuild(void Function(B builder) edit); | |
} | |
abstract class Builder<V extends Built<V, B>, B extends Builder<V, B>> { | |
V build(); | |
} | |
abstract class ListBuilder<V> { | |
V operator [](int index); | |
void operator []=(int index, V element); | |
} | |
class Value extends Built<Value, ValueBuilder> { | |
Value rebuild(void Function(ValueBuilder builder) edit) { | |
var b = ValueBuilder.from(this); | |
edit(b); | |
return b.build(); | |
} | |
} | |
class ValueBuilder extends Builder<Value, ValueBuilder> { | |
final Value _original; | |
ValueBuilder.from(Value value) : _original = value; | |
Value build() => _original; | |
} | |
extension ListBuilderRebuildExtension<V extends Built<V, B>, B extends Builder<V, B>> | |
on ListBuilder<Built<V, B>> { | |
void rebuildAt(int index, void Function(B) edit) { | |
this[index] = this[index].rebuild(edit); | |
} | |
} | |
class ListBuilderImpl<V> implements ListBuilder<V> { | |
List<V> _base = []..length = 10; | |
int get length => _base.length; | |
V operator [](int index) => _base[index]; | |
void operator []=(int index, V element) { | |
_base[index] = element; | |
} | |
} | |
void main() { | |
ListBuilder<Value> b = ListBuilderImpl(); | |
b[0] = Value(); | |
b.rebuildAt(0, (b) { | |
var argumentType = staticType(b); | |
print("$argumentType: ${b is ValueBuilder}"); | |
return; | |
}); | |
} | |
Type staticType<T>(T value) => T; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment