Skip to content
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

Return NaN after iteration #50

Closed
parzhitsky opened this issue May 9, 2020 · 1 comment · Fixed by #52
Closed

Return NaN after iteration #50

parzhitsky opened this issue May 9, 2020 · 1 comment · Fixed by #52
Labels
Change: patch [Issue / PR] describes a small non-breaking change, such as bugfix or an optimization Domain: main [Issue / PR] describes change in the functionality, its optimization good first issue [Issue] can be addressed by a first-time contributor Priority: medium [Issue / PR] should be addressed without delay Type: bug [Issue / PR] addresses malfunction

Comments

@parzhitsky
Copy link
Member

In definition of XRange type, change void to undefined, since void doesn't work for some reason.

- type XRange = Generator<number, void>;
+ type XRange = Generator<number, undefined>;

declare const range: XRange;

const nextValue: number | void = range.next().value;

const value: number = nextValue!;
- //          ^ Error!
- // Type 'number | void' is not assignable to type 'number'.
- //  Type 'void' is not assignable to type 'number'
+ // no errors
@parzhitsky parzhitsky added Change: patch [Issue / PR] describes a small non-breaking change, such as bugfix or an optimization Domain: main [Issue / PR] describes change in the functionality, its optimization Priority: medium [Issue / PR] should be addressed without delay Type: bug [Issue / PR] addresses malfunction good first issue [Issue] can be addressed by a first-time contributor labels May 9, 2020
@parzhitsky
Copy link
Member Author

It turns out that undefined also brings some problems:

- type XRange = Generator<number, void>;
+ type XRange = Generator<number, undefined>;

declare const range: XRange;

if (range.next().value < SOME_VALUE) act();
- // no errors
+ //            ^ Error!
+ // Object is possibly 'undefined'.

One way to solve it (although I don't really like it) is to return NaN after iteration, instead of undefined (both implicit and explicit), as in:

for (const number of numbers)
  yield number;

+ return NaN;

This would allow defining XRange as Generator<number, number>, and have no silly errors.

The reason I don't like this approach is because conceptually outside of any numeric range there are not only numbers, but no values at all, of any type. This idea would be properly conveyed by producing a void (undefined or null), while NaN means that there is something outside of a numeric range, it is just not a number, — which is silly, but here we are.

@parzhitsky parzhitsky changed the title Change void to undefined Return NaN after iteration May 11, 2020
parzhitsky added a commit to parzh/xrange__core that referenced this issue May 11, 2020
@parzhitsky parzhitsky linked a pull request May 11, 2020 that will close this issue
parzhitsky added a commit that referenced this issue May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change: patch [Issue / PR] describes a small non-breaking change, such as bugfix or an optimization Domain: main [Issue / PR] describes change in the functionality, its optimization good first issue [Issue] can be addressed by a first-time contributor Priority: medium [Issue / PR] should be addressed without delay Type: bug [Issue / PR] addresses malfunction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant