-
Notifications
You must be signed in to change notification settings - Fork 15
null safety #52
null safety #52
Changes from 7 commits
6af344c
1a3dbe6
0615111
0f788bc
8c08ac4
a6c49f1
b74808c
8658a2b
59a0991
df84a7b
f354b05
741d6d0
c02f50b
339abbb
2c454ea
d78bd80
b13c4eb
a19da5b
291cf40
6adde6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,13 @@ env: | |
|
||
jobs: | ||
# Check code formatting and static analysis on a single OS (linux) | ||
# against Dart dev. | ||
# against Dart beta. | ||
analyze: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
sdk: [dev] | ||
sdk: [beta] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: dart-lang/[email protected] | ||
|
@@ -38,7 +38,7 @@ jobs: | |
|
||
# Run tests on a matrix consisting of two dimensions: | ||
# 1. OS: ubuntu-latest, (macos-latest, windows-latest) | ||
# 2. release channel: dev | ||
# 2. release channel: beta | ||
test: | ||
needs: analyze | ||
runs-on: ${{ matrix.os }} | ||
|
@@ -47,7 +47,7 @@ jobs: | |
matrix: | ||
# Add macos-latest and/or windows-latest if relevant for this package. | ||
os: [ubuntu-latest] | ||
sdk: [2.3.0, dev] | ||
sdk: [beta] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: dart-lang/[email protected] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ final _empty = Future<Null>.value(null); | |
/// have not completed. If the [edges] callback needs to be limited or throttled | ||
/// that must be done by wrapping it before calling [crawlAsync]. | ||
Stream<V> crawlAsync<K, V>(Iterable<K> roots, FutureOr<V> Function(K) readNode, | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
FutureOr<Iterable<K>> Function(K, V) edges) { | ||
FutureOr<Iterable<K>?> Function(K, V) edges) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @natebosch I am a bit surprised we allow returning null here? should we retain that behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I think we should restrict it - we previously allowed I'm on the fence about statically typing it as non-nullable, but dynamically allowing null to maintain backwards compatibility for opt-out code. Even with a major version bump it might be friendly to do so. |
||
final crawl = _CrawlAsync(roots, readNode, edges)..run(); | ||
return crawl.result.stream; | ||
} | ||
|
@@ -43,7 +43,7 @@ class _CrawlAsync<K, V> { | |
final result = StreamController<V>(); | ||
|
||
final FutureOr<V> Function(K) readNode; | ||
final FutureOr<Iterable<K>> Function(K, V) edges; | ||
final FutureOr<Iterable<K>?> Function(K, V) edges; | ||
final Iterable<K> roots; | ||
|
||
final _seen = HashSet<K>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,12 @@ import 'dart:collection'; | |
/// | ||
/// If you supply one of [equals] or [hashCode], you should generally also to | ||
/// supply the other. | ||
List<T> shortestPath<T>( | ||
List<T>? shortestPath<T>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My gut reaction is that most of these should be |
||
T start, | ||
T target, | ||
Iterable<T> Function(T) edges, { | ||
bool Function(T, T) equals, | ||
int Function(T) hashCode, | ||
bool Function(T?, T?)? equals, | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int Function(T)? hashCode, | ||
}) => | ||
_shortestPaths<T>( | ||
start, | ||
|
@@ -61,8 +61,8 @@ List<T> shortestPath<T>( | |
Map<T, List<T>> shortestPaths<T>( | ||
T start, | ||
Iterable<T> Function(T) edges, { | ||
bool Function(T, T) equals, | ||
int Function(T) hashCode, | ||
bool Function(T?, T?)? equals, | ||
int Function(T)? hashCode, | ||
}) => | ||
_shortestPaths<T>( | ||
start, | ||
|
@@ -74,29 +74,28 @@ Map<T, List<T>> shortestPaths<T>( | |
Map<T, List<T>> _shortestPaths<T>( | ||
T start, | ||
Iterable<T> Function(T) edges, { | ||
T target, | ||
bool Function(T, T) equals, | ||
int Function(T) hashCode, | ||
T? target, | ||
bool Function(T?, T?)? equals, | ||
int Function(T)? hashCode, | ||
}) { | ||
assert(start != null, '`start` cannot be null'); | ||
assert(edges != null, '`edges` cannot be null'); | ||
|
||
final distances = HashMap<T, List<T>>(equals: equals, hashCode: hashCode); | ||
distances[start] = const []; | ||
|
||
equals ??= _defaultEquals; | ||
if (equals(start, target)) { | ||
if (equals.call(start, target)) { | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return distances; | ||
} | ||
|
||
final toVisit = ListQueue<T>()..add(start); | ||
|
||
List<T> bestOption; | ||
List<T>? bestOption; | ||
|
||
while (toVisit.isNotEmpty) { | ||
final current = toVisit.removeFirst(); | ||
final currentPath = distances[current]; | ||
final currentPathLength = currentPath.length; | ||
final currentPathLength = currentPath?.length ?? 0; | ||
|
||
if (bestOption != null && (currentPathLength + 1) >= bestOption.length) { | ||
// Skip any existing `toVisit` items that have no chance of being | ||
|
@@ -111,13 +110,13 @@ Map<T, List<T>> _shortestPaths<T>( | |
assert(existingPath == null || | ||
existingPath.length <= (currentPathLength + 1)); | ||
|
||
if (existingPath == null) { | ||
if (existingPath == null && currentPath != null) { | ||
final newOption = [ | ||
...currentPath, | ||
edge, | ||
]; | ||
|
||
if (equals(edge, target)) { | ||
if (target != null && equals(edge, target)) { | ||
assert(bestOption == null || bestOption.length > newOption.length); | ||
bestOption = newOption; | ||
} | ||
|
@@ -135,4 +134,4 @@ Map<T, List<T>> _shortestPaths<T>( | |
return distances; | ||
} | ||
|
||
bool _defaultEquals(Object a, Object b) => a == b; | ||
bool _defaultEquals<T>(T a, T b) => a == b; | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
name: graphs | ||
version: 0.2.1-dev | ||
version: 0.3.0-nullsafety.0 | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: Graph algorithms that operate on graphs in any representation | ||
homepage: https://github.com/dart-lang/graphs | ||
|
||
environment: | ||
sdk: '>=2.3.0 <3.0.0' | ||
|
||
sdk: '>=2.12.0-0 <3.0.0' | ||
dependency_overrides: | ||
hlystovov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
analyzer: ^0.42.0-nullsafety.0 | ||
dev_dependencies: | ||
pedantic: ^1.3.0 | ||
test: ^1.5.1 | ||
pedantic: ^1.10.0 | ||
test: ^1.16.0 | ||
|
||
# For examples | ||
analyzer: '>=0.35.0 <0.42.0' | ||
path: ^1.1.0 | ||
pool: ^1.3.0 | ||
path: ^1.8.0 | ||
pool: ^1.5.0 |
Uh oh!
There was an error while loading. Please reload this page.