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

Need something more robust than polymorphic this typing for RxJS #5845

Closed
benlesh opened this issue Dec 1, 2015 · 6 comments
Closed

Need something more robust than polymorphic this typing for RxJS #5845

benlesh opened this issue Dec 1, 2015 · 6 comments
Labels
Discussion Issues which may not have code impact Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented.

Comments

@benlesh
Copy link

benlesh commented Dec 1, 2015

I was pretty excited about this typing for RxJS, since it does have a sort of "fluent" style interface. However we have some issues with it that are discussed here: ReactiveX/rxjs#846

Basically, we have operators that return same-shaped, but different generic types:

map for example: Observable<T>.prototype.map<R>((value: T) => R): Observable<R>

The reason we were looking at this typing the above is that map doesn't always return an Observable, it could be called on a Subject which is a subclass of Observable. In that case, it actually returns a Subject, not an Observable, because of our fancy lift mechanism:

The Observable's lift:
https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/Observable.ts#L51-L63

The Subject's lift:
https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/Subject.ts#L37-L41

How lift is used (in map for example):
https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/operators/map.ts#L17

It would be fantastic if we could provide our TypeScript users with the proper type information in their IDEs. Perhaps some additional syntax like this<R> would fit the bill? It's also worth noting that operators always return a new instance, rather than the same this instance, if that makes a difference.

@DanielRosenwasser DanielRosenwasser added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Discussion Issues which may not have code impact labels Dec 1, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 4, 2015

I think this is another instance of the higher order generic referenced in: #1213 combined with this types. i think you want to say something like:

map<T<*> extends Observable<*>, R, U>(this: T<U>, fn: (value: U) => R): T<R>

@benlesh
Copy link
Author

benlesh commented Dec 4, 2015

@mhegazy that looks something like it yeah. My C# is rusty, but I think in C# it was something like map<T, R, U>(this: T<U>, fn: (value: U) => R) where T<U> : Observable<U> or the like.

@sandersn
Copy link
Member

sandersn commented Dec 4, 2015

I'm pretty sure C# doesn't support higher-kinded types either, so it'll fail at T<U>.

@sandersn sandersn added the Duplicate An existing issue was already created label Dec 4, 2015
@sandersn sandersn closed this as completed Dec 4, 2015
@weswigham
Copy link
Member

No - C# does not yet have higher-kinded types.

@benlesh
Copy link
Author

benlesh commented Dec 4, 2015

@weswigham bummer. Not that I think it would be too useful in C# in most cases. But since TypeScript has JavaScript as a build target, I think it would be useful here. Well, clearly, because we've hit the need.

@diab0l
Copy link

diab0l commented Apr 15, 2016

@mhegazy, @Blesh

for your example you do not need higher kinded types, f-bounded polymorphism is sufficient
your example can be expressed in C# as
static T map<T, R, U>(this T @this, Func<U, R> fn) where T : Observable<U> { ... }
which is an extension method

and in typescript (since 1.8 I think) as
map<T extends Observable<U>, R, U>(that: T, fn: (value: U) => R): T { ... }
which is unfortunately an ordinary function

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Discussion Issues which may not have code impact Duplicate An existing issue was already created Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented.
Projects
None yet
Development

No branches or pull requests

6 participants