Skip to content

Commit

Permalink
fix(*): remove self-types from interfaces
Browse files Browse the repository at this point in the history
This patches 7a63ffe
In Typescript 2.5, some changes were made that broke checking self-types
in interfaces.  This is unfortunate in that it widens the `getOrElse`
and `orElse` interface, but does unbreak Monapt on 2.5+.

Pull requests to fix this welcome!

Fixes #20
  • Loading branch information
jklmli committed Jan 16, 2018
1 parent bf4b9ee commit 7e13c4c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/option/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class None_<A> implements Option<A> {
throw new Option.NoSuchElementError();
}

getOrElse<B, A extends B>(this: Option<A>, defaultValue: () => B): B {
getOrElse<B, A extends B>(this: None_<A>, defaultValue: () => B): B {
return defaultValue();
}

Expand All @@ -59,7 +59,7 @@ class None_<A> implements Option<A> {
return matcher.None();
}

orElse<B, A extends B>(this: Option<A>, alternative: () => Option<B>): Option<B> {
orElse<B, A extends B>(this: None_<A>, alternative: () => Option<B>): Option<B> {
return alternative();
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Some } from './some';
/**
* An Option represents a collection of size 0 or 1. Traditionally it's used as a replacement for
* `null` or `undefined` to explicitly encode if a value can be empty or not.
*
* :TODO: Narrow self-type in `getOrElse` and `orElse` interfaces.
*/
interface Option<A> {
isDefined: boolean;
Expand All @@ -15,10 +17,10 @@ interface Option<A> {
flatMap<B>(flatMapper: (value: A) => Option<B>): Option<B>;
foreach(run: (value: A) => void): void;
get(): A;
getOrElse<B, A extends B>(this: Option<A>, defaultValue: () => B): B;
getOrElse<B>(defaultValue: () => B): B;
map<B>(mapper: (value: A) => B): Option<B>;
match<B>(matcher: { Some: (a: A) => B, None: () => B }): B;
orElse<B, A extends B>(this: Option<A>, alternative: () => Option<B>): Option<B>;
orElse<B>(alternative: () => Option<B>): Option<B>;
}

/* tslint:disable:no-null-keyword only-arrow-functions */
Expand Down
12 changes: 4 additions & 8 deletions src/option/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ class Some<A> implements Option<A> {
return this.value;
}

getOrElse<B, A extends B>(this: Option<A>, defaultValue: () => B): B {
const self: Some<A> = this as Some<A>;

return self.value;
getOrElse<B, A extends B>(this: Some<A>, defaultValue: () => B): B {
return this.value;
}

map<B>(mapper: (value: A) => B): Option<B> {
Expand All @@ -67,10 +65,8 @@ class Some<A> implements Option<A> {
return matcher.Some(this.value);
}

orElse<B, A extends B>(this: Option<A>, alternative: () => Option<B>): Option<B> {
const self: Some<A> = this as Some<A>;

return self;
orElse<B, A extends B>(this: Some<A>, alternative: () => Option<B>): Option<B> {
return this;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/try/failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Failure<A> implements Try<A> {
throw this.error;
}

getOrElse<B, A extends B>(this: Try<A>, defaultValue: () => B): B {
getOrElse<B, A extends B>(this: Failure<A>, defaultValue: () => B): B {
return defaultValue();
}

Expand All @@ -57,7 +57,7 @@ class Failure<A> implements Try<A> {
return matcher.Failure(this.error);
}

orElse<B, A extends B>(this: Try<A>, alternative: () => Try<B>): Try<B> {
orElse<B, A extends B>(this: Failure<A>, alternative: () => Try<B>): Try<B> {
return alternative();
}

Expand Down
12 changes: 4 additions & 8 deletions src/try/success.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ class Success<A> implements Try<A> {
return this.value;
}

getOrElse<B, A extends B>(this: Try<A>, defaultValue: () => B): B {
const self: Success<A> = this as Success<A>;

return self.value;
getOrElse<B, A extends B>(this: Success<A>, defaultValue: () => B): B {
return this.value;
}

map<B>(f: (value: A) => B): Try<B> {
Expand All @@ -60,10 +58,8 @@ class Success<A> implements Try<A> {
return matcher.Success(this.value);
}

orElse<B, A extends B>(this: Try<A>, alternative: () => Try<B>): Try<B> {
const self: Success<A> = this as Success<A>;

return self;
orElse<B, A extends B>(this: Success<A>, alternative: () => Try<B>): Try<B> {
return this;
}

recover(fn: (error: Error) => A): Try<A> {
Expand Down
6 changes: 4 additions & 2 deletions src/try/try.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Success } from './success';

/**
* A Try represents a computation that may or may not have succeeded.
*
* :TODO: Narrow self-type in `getOrElse` and `orElse` interfaces.
*/
interface Try<A> {
isFailure: boolean;
Expand All @@ -13,10 +15,10 @@ interface Try<A> {
flatMap<B>(f: (value: A) => Try<B>): Try<B>;
foreach(run: (value: A) => void): void;
get(): A;
getOrElse<B, A extends B>(this: Try<A>, defaultValue: () => B): B;
getOrElse<B>(defaultValue: () => B): B;
map<B>(f: (value: A) => B): Try<B>;
match<B>(matcher: { Success: (a: A) => B, Failure: (e: Error) => B }): B;
orElse<B, A extends B>(this: Try<A>, alternative: () => Try<B>): Try<B>;
orElse<B>(alternative: () => Try<B>): Try<B>;
recover(fn: (error: Error) => A): Try<A>;
recoverWith(fn: (error: Error) => Try<A>): Try<A>;
toOption(): Option<A>;
Expand Down

0 comments on commit 7e13c4c

Please sign in to comment.