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

this keyword as a dynamic return type #147

Open
Mouvedia opened this issue Jun 19, 2019 · 4 comments
Open

this keyword as a dynamic return type #147

Mouvedia opened this issue Jun 19, 2019 · 4 comments

Comments

@Mouvedia
Copy link
Collaborator

Mouvedia commented Jun 19, 2019

related: #68

example

You have a fluent interface but one of the method X is slightly restrictive and doesn't return this but a subset of its this. In this case if the next method Y returns this it will share its constraints.

problem: if that method Y may also follow another method Z that returns this, its return type becomes dynamic.
solution: introduce a this return type

NB

It would be impractical to require the user to provide all the possibilities as unions.
It's the job of the static type checker; it should be able to infer the correct type from the current this.

proposal

example(name: Type) => this
@ericelliott
Copy link
Owner

ericelliott commented Jun 19, 2019

Wasn't this resolved with the :: proposal?

ThisType::example(name: Type) => ThisType

And, if you want to document changes:

ThisType::example(name: Type) => ThisType & { foo: String }

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Jun 19, 2019

I assume that ThisType is akin to Foo and not your proposal for this

@ericelliott I am talking about a dynamic type. As explained here:

…granted that you have declared the newly bound interface.

As I said in the nota bene up there that would require to return unions. Also your example can't work precisely because the signature of the function that I am talking about is not bound (i.e it may have different call-sites).

What are our priorities? Are the use cases for this feature too rare?

PRO

  • tools would infer the right type depending on the current this
  • avoids having to list/declare every single interface that are ever so slightly different

CON

  • murkier documentation
  • an union would do

@ericelliott
Copy link
Owner

ericelliott commented Jun 19, 2019

ThisType is a type variable. It does not need to be declared.

Essentially, it is the same thing as this, and even without explicitly adding a special this type, you could just use it, and it would be treated as a type variable:

this::method() => this

But you need the :: to use it. That's how we know you're working with this.

@Mouvedia
Copy link
Collaborator Author

Mouvedia commented Jun 20, 2019

But you need the :: to use it. That's how we know you're working with this.

I like this, it's explicit.

ThisType is a type variable.

I thought we encouraged type variables to be one char long:

By convention, type variables are single letters and lowercased…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants