Skip to content

Commit

Permalink
Enabled strict null checking
Browse files Browse the repository at this point in the history
Note this version generates some errors, all of which seem valid to me.  If we adopt this, it seems like it could replace the @NotNull decorator.
  • Loading branch information
Burt Harris authored and sharwell committed Oct 10, 2016
1 parent 403cd82 commit 9fabbf5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
22 changes: 11 additions & 11 deletions src/misc/Array2DHashSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ const LOAD_FACTOR: number = 0.75;

export class Array2DHashSet<T> implements JavaSet<T> {
@NotNull
protected comparator: EqualityComparator<T>;
protected comparator: EqualityComparator<T> | null;

protected buckets: T[][];
protected buckets: (T|undefined)[][];

/** How many elements in set */
protected n: number = 0;
Expand All @@ -65,7 +65,7 @@ export class Array2DHashSet<T> implements JavaSet<T> {

constructor(
@Nullable
comparator: EqualityComparator<T> = null,
comparator: EqualityComparator<T> | null = null,
initialCapacity: number = INITAL_CAPACITY,
initialBucketCapacity: number = INITAL_BUCKET_CAPACITY) {

Expand All @@ -86,7 +86,7 @@ export class Array2DHashSet<T> implements JavaSet<T> {

protected getOrAddImpl(o: T): T {
let b: number = this.getBucket(o);
let bucket: T[] = this.buckets[b];
let bucket = this.buckets[b];

// NEW BUCKET
if ( bucket==null ) {
Expand All @@ -99,7 +99,7 @@ export class Array2DHashSet<T> implements JavaSet<T> {

// LOOK FOR IT IN BUCKET
for (let i=0; i<bucket.length; i++) {
let existing: T = bucket[i];
let existing = bucket[i];
if ( existing==null ) { // empty slot; not there, add.
bucket[i] = o;
this.n++;
Expand All @@ -116,10 +116,10 @@ export class Array2DHashSet<T> implements JavaSet<T> {
return o;
}

get(o: T): T {
get(o: T): T | null {
if ( o==null ) return o;
let b: number = this.getBucket(o);
let bucket: T[] = this.buckets[b];
let b: number = this.getBucket(o);
let bucket = this.buckets[b];
if ( bucket==null ) return null; // no bucket
for (let e of bucket) {
if ( e==null ) return null; // empty slot; not there
Expand Down Expand Up @@ -159,7 +159,7 @@ export class Array2DHashSet<T> implements JavaSet<T> {
}

protected expand(): void {
let old: T[][] = this.buckets;
let old = this.buckets;
this.currentPrime += 4;
let newCapacity: number = this.buckets.length * 2;
let newTable: T[][] = this.createBuckets(newCapacity);
Expand Down Expand Up @@ -273,14 +273,14 @@ export class Array2DHashSet<T> implements JavaSet<T> {
}

let b: number = this.getBucket(obj);
let bucket: T[] = this.buckets[b];
let bucket: (T|undefined)[] = this.buckets[b];
if ( bucket==null ) {
// no bucket
return false;
}

for (let i=0; i<bucket.length; i++) {
let e: T = bucket[i];
let e: T | undefined = bucket[i];
if ( e==null ) {
// empty slot; not there
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/misc/Stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ class IterableAdapter<T> implements Iterable<T>, IterableIterator<T> {
[Symbol.iterator]() { this._iterator = this.collection.iterator(); return this;}

next(): IteratorResult<T> {
if (!this._iterator.hasNext()) return { done: true, value: undefined };
if (!this._iterator.hasNext()) {
// A bit of a hack needed here, tracking under https://github.com/Microsoft/TypeScript/issues/11375
return { done: true, value: undefined } as any as IteratorResult<T>;
}
return {done: false, value: this._iterator.next()}
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "commonjs",
"target": "es2015",
"noImplicitAny": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"declaration": true,
"preserveConstEnums": true,
Expand Down

0 comments on commit 9fabbf5

Please sign in to comment.