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

Add new annotation: @psalm-self-out #3650

Merged
merged 24 commits into from
Jul 1, 2020
Merged

Conversation

olleharstedt
Copy link
Contributor

@olleharstedt olleharstedt commented Jun 23, 2020

This PR adds the annotation @param-self-out for methods, changing the type of $this.

Use-case is to be able to create your own typestates besides the ones already implicit in Psalm (resource, and closed-resource) in an object-oriented manner.

Typestates are mostly (only?) useful together with alias tracking or ownership inference, which is another discussion: #3614

@muglug
Copy link
Collaborator

muglug commented Jun 23, 2020

This is a good start. I’d like Psalm to also be able to verify the integrity of @psalm-self-out just as it does for @param-out.

@olleharstedt
Copy link
Contributor Author

This is a good start. I’d like Psalm to also be able to verify the integrity of @psalm-self-out just as it does for @param-out.

Agree.

}
class Bar {
/**
* @psalm-self-out Foo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Foo&Bar, really - and could you add an exception if the function doesn’t do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but maybe let's detail a little bit what we mean? Using the closed/open file as example:

interface ClosedFile {
  /**
   * @psalm-self-out File&OpenFile
   */
  public function open() {}
}
interface OpenFile {
  /**
   * @psalm-self-out File&ClosedFile
   */
  public function close() {}
  public function read() {}
}
class File implements OpenFile, ClosedFile {
  // ... function defs here
}

Then we define psalm-self-out as always requiring first part of the intersection type being the base-class, second part being the "next" state. This way, Psalm doesn't need to "remember" the base-class in a separate field.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in this case File is already File&OpenFile&ClosedFile, by virtue of implementing those interfaces, isn't it? Or do you propose to reuse the same syntax to achieve different effects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right. Hm, so maybe intersection types cannot be used.

Or do you propose to reuse the same syntax to achieve different effects?

No no no.

@olleharstedt
Copy link
Contributor Author

Fun fact: In Smalltalk something similar is called become: https://gbracha.blogspot.com/2009/07/miracle-of-become.html

@olleharstedt
Copy link
Contributor Author

@muglug Hm, how can I debug Circle CI without giving it access to absolutely everything on my account?

@olleharstedt
Copy link
Contributor Author

Only thing left failing is taint tests, it seems.

@muglug
Copy link
Collaborator

muglug commented Jun 30, 2020

Only thing left failing is taint tests, it seems.

Ignore that, but would you mind squashing these commits?

@olleharstedt
Copy link
Contributor Author

olleharstedt commented Jul 1, 2020

Only thing left failing is taint tests, it seems.

Ignore that, but would you mind squashing these commits?

Can't do it without rebasing everthing. Can't you squash and merge instead if/when this PR gets accepted?

The tab "Files changed" contains all changes for all commits in this PR, if you want an overview.

@muglug muglug merged commit d8e8ce4 into vimeo:master Jul 1, 2020
@muglug
Copy link
Collaborator

muglug commented Jul 1, 2020

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants