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

Introduce top type #1729

Closed
bdsl opened this issue Jun 3, 2019 · 6 comments
Closed

Introduce top type #1729

bdsl opened this issue Jun 3, 2019 · 6 comments

Comments

@bdsl
Copy link
Contributor

bdsl commented Jun 3, 2019

Psalm has a bottom type, no-return, and the dynamic type mixed, but I think it might be useful to also have a top type. Taking a lead from typescript, mixed is comparable to Typescript's any, no-return is comparable to Typescript's never, and we could adopt the name unknown for the top type.

To illustrate the difference, psalm emits MixedMethodCall on the call to bar() below:

<?php

/** @return mixed */
function foo() {
    return "bar";
}

function usesFoo(): void {
  foo()->bar();
}

MixedMethodCall is (with default Psalm settings) an INFO, not an ERROR.

However if the return statement was changed to /** @return unknown */ then instead of MixedMethodCall psalm would emit some form of error.

The top type should be treated as a supertype of all other types, so that the following code should cause no issues:

<?php
/** @param unknown $value */
function is_valid_array_key($value): bool {
    return is_string($value) || is_int($value);
}

unknown would be roughly equivilent to object|scalar|mixed|array|null, except currently psalm seems to treat object as a form of mixed, rather than as a supertype of all other object types.

@muglug
Copy link
Collaborator

muglug commented Jun 3, 2019

Flow handles this with a mixed type that's equivalent to TypeScript's unknown.

I think it's an interesting idea, but I wonder whether it would be useful in that many places.

@bdsl
Copy link
Contributor Author

bdsl commented Jun 3, 2019

Right, Hack does the same thing. But I think for PHP mixed probably has to retain its current meaning as used in existing docblocks.

If you don't have something like any / mixed then I think the language stops being gradually typed and becomes statically typed, which we probably don't want.

@ondrejmirtes
Copy link

PHPStan's mixed is effectively a top type.

@muglug
Copy link
Collaborator

muglug commented Jun 4, 2019

PHPStan's mixed is effectively a top type.

The only deliberate difference I'm aware of is that a union of mixed|string in Psalm is still mixed|string.

The benefit of that is in finding bugs - PossiblyInvalidMethodCall can be emitted for https://psalm.dev/r/80910f8ebf where previously it would just be a MixedMethodCall issue.

@ondrejmirtes
Copy link

Yes, I want to tackle this by adding this as a property to MixedType - "it's still mixed, but sometimes I'm sure it's a string".

@muglug
Copy link
Collaborator

muglug commented Nov 3, 2020

Closing as not really relevant

@muglug muglug closed this as completed Nov 3, 2020
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

3 participants