Skip to content

Commit

Permalink
raise warnings when of is called on Left or Right classes
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Feb 25, 2018
1 parent 1372368 commit c884086
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions appendix_b.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class Left extends Either {
return false;
}

static of(x) {
throw new Error('`of` called on class Left (value) instead of Either (type)');
}

inspect() {
return `Left(${inspect(this.$value)})`;
}
Expand Down Expand Up @@ -104,6 +108,10 @@ class Right extends Either {
return true;
}

static of(x) {
throw new Error('`of` called on class Right (value) instead of Either (type)');
}

inspect() {
return `Right(${inspect(this.$value)})`;
}
Expand Down
8 changes: 4 additions & 4 deletions ch08.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Left extends Either {

class Right extends Either {
map(f) {
return Right.of(f(this.$value));
return Either.of(f(this.$value));
}

inspect() {
Expand Down Expand Up @@ -572,10 +572,10 @@ Even with `Task`, our `IO` and `Either` functors are not out of a job. Bear with
// dbUrl :: Config -> Either Error Url
const dbUrl = ({ uname, pass, db }) => {
if (uname && pass && host && db) {
return Right.of(`db:pg://${uname}:${pass}@${host}5432/${db}`);
return Either.of(`db:pg://${uname}:${pass}@${host}5432/${db}`);
}

return Left.of(Error('Invalid config!'));
return left(Error('Invalid config!'));
};

// connectDb :: Config -> Either Error (IO DbConnection)
Expand Down Expand Up @@ -666,7 +666,7 @@ We can instantly see and refactor code based on properties held by all functors.
Functors can stack:

```js
const nested = Task.of([Right.of('pillows'), Left.of('no sleep for you')]);
const nested = Task.of([Either.of('pillows'), left('no sleep for you')]);

map(map(map(toUpperCase)), nested);
// Task([Right('PILLOWS'), Left('no sleep for you')])
Expand Down

0 comments on commit c884086

Please sign in to comment.