-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISortedSet #7
Comments
I'm not sure if I'm following what you mean, could you please give me some extra help? Is this a bug report or a feature request? As far as I know, FIC currently does memorize your configuration for collections. So you don't need to reconfigure your collection after applying an operation on it and getting a new object. Take the fast_immutable_collections/test/iset/iset_test.dart Lines 560 to 575 in 333c4ed
So FIC operations do carry the current configuration along. Besides that, you could also set different global configurations with the I believe you actually mean it would be better to, instead of having one single At any rate, even though I don't think I fully understood the question or issue, I would ask you to maybe post an extra example without the extra dependency of |
@opsb Question: Do you want to accept any ISet (or any Iterable) and then convert them to sorted, or do you want to allow only sorted ISets to be passed into your constructor? |
@marcglasberg I want to ask the compiler to enforce that a field always be a sorted set (rather than be a field which can either be sorted or not sorted). I used freezed for the original example because the immutable record style naturally leads to this sort of requirement. If I use an ISet then this isn't possible, see the following example (using freezed again because I think it gives useful context) @freezed
abstract class DataBatch with _$DataBatch {
factory DataBatch({
@required ISet<int> sortedNumbers,
}) = _DataBatch;
}
var batch = DataBatch({sortedNumbers: ISet([1,5,3], ConfigSet(sort: true))});
print(batch);
// [1,3,5]
var updatedBatch = batch.copyWith(sortedNumbers: ISet(1, 5, 3));
print(updatedBatch);
// [1,5,3] Here's the same example but using @freezed
abstract class DataBatch with _$DataBatch {
factory DataBatch({
@required ISortedSet<int> sortedNumbers,
}) = _DataBatch;
}
var batch = DataBatch({sortedNumbers: SortedISet([1,5,3]});
print(batch);
// [1,3,5]
var updatedBatch = batch.copyWith(sortedNumbers: ISet(1, 5, 3)); // compilation error
var updatedBatch = batch.copyWith(sortedNumbers: ISet.withConfig(1, 5, 3, ConfigSet(sort: false))); // compilation error
var updatedBatch = batch.copyWith(sortedNumbers: ISet.withConfig(1, 5, 3, ConfigSet(sort: true))); // compilation error
var updatedBatch = batch.copyWith(sortedNumbers: ISortedSet(1, 5, 3)); // compiles The compilation error in each of those cases being that an |
yes that's exactly right, I'm requesting |
Your freezed example makes it harder for me to understand the problem because it's a package I've never used. Suppose I was to create class TimelineChartData {
ISet<GlucoseReading> glucoseReadings;
ISet<InsulinInjection> insulinInjections;
TimelineChartData({
@required Extent<DateTime> focusedRange,
@required Iterable<GlucoseReading> glucoseReadings,
@required Iterable<InsulinInjection> insulinInjections,
}) : glucoseReadings = glucoseReadings.toISetSorted(),
insulinInjections = insulinInjections.toISetSorted(); Writing However, if you do this: class TimelineChartData {
ISet<GlucoseReading> glucoseReadings;
ISet<InsulinInjection> insulinInjections;
TimelineChartData({
@required Extent<DateTime> focusedRange,
@required ISortedSet<GlucoseReading> glucoseReadings,
@required ISortedSet<InsulinInjection> insulinInjections,
}); Then in this case you can only pass it an immutable sorted set. |
I wasn't sure if your last example was supposed to be class TimelineChartData {
ISortedSet<GlucoseReading> glucoseReadings;
ISortedSet<InsulinInjection> insulinInjections;
TimelineChartData({
@required Extent<DateTime> focusedRange,
@required ISortedSet<GlucoseReading> glucoseReadings,
@required ISortedSet<InsulinInjection> insulinInjections,
}); if so then I think we're on the same page and you can ignore the rest of my post. I realise that using freezed for my examples has made my motivation for ISortedSet a little opaque so I've used your examples to expand on this. While constructors may be used to require that an Taking your first example: class TimelineChartData {
ISet<GlucoseReading> glucoseReadings;
ISet<InsulinInjection> insulinInjections;
TimelineChartData({
@required Extent<DateTime> focusedRange,
@required Iterable<GlucoseReading> glucoseReadings,
@required Iterable<InsulinInjection> insulinInjections,
}) : glucoseReadings = glucoseReadings.toISetSorted(),
insulinInjections = insulinInjections.toISetSorted(); If we construct a new object... var chartData = TimelineChartData(
focusedRange: focusedRange,
glucoseReadings: glucoseReadings.toList(),
insulinInjections: insulinInjections.toList(),
); ...then the constructor will convert However if we then update the property on the chartData... chartData.glucoseReadings = ISet(updatedGlucoseReadings); ...then class TimelineChartData {
ISortedSet<GlucoseReading> glucoseReadings; ...the compiler is able to guarantee that the set is sorted so that the following is a compilation error chartData.glucoseReadings = ISet(updatedGlucoseReadings); |
I'm sorry, what I actually should have written is: @immutable
class TimelineChartData {
final ISet<GlucoseReading> glucoseReadings;
final ISet<InsulinInjection> insulinInjections;
TimelineChartData({
@required Extent<DateTime> focusedRange,
@required Iterable<GlucoseReading> glucoseReadings,
@required Iterable<InsulinInjection> insulinInjections,
}) : glucoseReadings = glucoseReadings.withConfig(ConfigSet(sort:true)),
insulinInjections = insulinInjections.withConfig(ConfigSet(sort:true)), I am assuming Of course, if But please note, the sorted/unsorted configuration is only one of the possible configurations for a set. |
yeah, this had occurred to me and I've been pondering how best to encode all of those options in the type system. One approach might be to make it easy for developers to subclass ISet and pass the configuration up to the super constructor (or similar approach with an overridden method that returned the config). Another approach might be phantom types but you'd need something like freezed for the ADTs and I suspect would be unfamiliar to Dart developers. |
It is already easy to subclass ISet etc. Maybe I should just provide all combinations. |
I'm new to Dart so I may well be missing something simple, could you give an example of how to subclass |
Classes |
Thanks for the pointer @marcglasberg. I was seeing some bugs today relating to the sort order being lost in some places. I switched to using the following instead of a straight class IOrderedSet<T>
with FromISetMixin<T, IOrderedSet<T>>, FromIterableISetMixin<T>
implements Iterable<T> {
final ISet<T> _items;
IOrderedSet([Iterable<T> items = const []])
: _items = ISet.withConfig(items, ConfigSet(sort: true));
@override
ISet<T> get iter => _items;
@override
newInstance(ISet<T> iset) {
return IOrderedSet(iset);
}
@override
T elementAt(int index) => this[index];
T operator [](int index) {
return _items[index];
}
} Do you see any issues with this implementation? (edit: I really should have called it ISortedSet 😂) |
I don't see any issues with your implementation. Whatever works for you. Please note, your constructor now accepts any I will leave this issue open because I want to add |
I was very happy to find that https://pub.dev/documentation/fast_immutable_collections/latest/fast_immutable_collections/ISet-class.html can be configured to be sorted. In practice though I'm finding it difficult to ensure that a property that is specified as ISet is forced to remain sorted.
In this example you can see from the factory
TimelineChartData.from
that I want bothglucoseReadings
andinsulinInjections
to be sorted. The problem is that whenever I replaceglucoseReadings
orinsulinInjections
I need to remember to configure theISet
to be sorted. By introducingISortedSet
it would allow the compiler to enforce that the set remains sorted.As an aside thankyou very much for this package, dart deserves a quality immutable package and FIC seems to fill this role very nicely.
The text was updated successfully, but these errors were encountered: