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

Feature/variadic union type improvemence #184

Merged
merged 6 commits into from
Apr 27, 2021

Conversation

nzour
Copy link
Contributor

@nzour nzour commented Apr 26, 2021

#183

Changelog

  • function Psl\Type\union now takes variadic third parameter
  • function Psl\Type\union arguments $left_type and $right_type were renamed to $first and $second
    This is a breaking change, because if somebody uses PHP8+ and does
    union(left_type: $left, right_type: $right) - this would trigger runtime Error.

Future scope

  • I think we should use @no-named-arguments psalm's annotation
    I haven't used this in my PR, cause I think it should be discussed and (if accepted) added atomically for all possible functions and methods
  • As @azjezz mentioned in Union for variadic types #183, we should also make variadic function Psl\Type\intersection

@nzour
Copy link
Contributor Author

nzour commented Apr 26, 2021

I think I've chosen wrong branch to merge to. @azjezz do you have 2.0 branch?

@azjezz
Copy link
Owner

azjezz commented Apr 26, 2021

As mentioned in the issue, renaming parameters is not considered a BC break ( for me ), we can merge this into 1.7

@azjezz
Copy link
Owner

azjezz commented Apr 26, 2021

@zour228 can you apply the same change to intersection? I think these two should stay consistent with each other.

@nzour
Copy link
Contributor Author

nzour commented Apr 26, 2021

@zour228 can you apply the same change to intersection? I think these two should stay consistent with each other.

Sure, but maybe next day, it's late time on my watch

@nzour
Copy link
Contributor Author

nzour commented Apr 26, 2021

About named parameters and BC:
I still think it's important to mention parameters renaming in changelog.
To prevent unexpected error that somebody may have

@nzour
Copy link
Contributor Author

nzour commented Apr 26, 2021

@azjezz didn't know that psalm has an issues with intersection types (even with such a simple example)
https://psalm.dev/r/737e004fcd

Therefore the following example have type inference issues too:

$first = shape(['first' => string()]);
$second = shape(['second' => string()]);

$res = intersection($first, $second);
// inferred type of $res is TypeInterface<array{first: string}>
// but I was expecting TypeInterface<array{first: string, second: string}>

So, I am not sure if I know hot to implement function with variadic parameters.
But maybe type inference is not main feature here?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/737e004fcd
<?php

/**
 * @template Tf
 * @template Ts
 *
 * @param Tf $first
 * @param Ts $second
 *
 * @return Tf&Ts
 */
function intersection(
    mixed $first,
    mixed $second
): mixed {
    throw new RuntimeException();
}


$first = ['f' => 1];
$second = ['s' => 2];

/** @psalm-trace $result1 */
$result1 = intersection($first, $second);

/** @psalm-trace $result2 */
$result2 = intersection($second, $first);
Psalm output (using commit 698adef):

INFO: Trace - 24:1 - $result1: array{f: 1}

INFO: Trace - 27:1 - $result2: array{s: 2}

@azjezz
Copy link
Owner

azjezz commented Apr 27, 2021

Intersection types work only with objects

@coveralls
Copy link

Pull Request Test Coverage Report for Build 787060807

  • 5 of 5 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 744011924: 0.0%
Covered Lines: 2985
Relevant Lines: 2985

💛 - Coveralls

Copy link
Owner

@azjezz azjezz left a comment

Choose a reason for hiding this comment

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

LGTM, you can send a separate PR for intersection types later on.

Thank you 🎉

@azjezz azjezz merged commit e187467 into azjezz:1.7.x Apr 27, 2021
nzour added a commit to nzour/psl that referenced this pull request Apr 27, 2021
[Type] add support for variadic union types (azjezz#184)
@azjezz azjezz added this to the 1.7.0 milestone Apr 27, 2021
@azjezz azjezz added Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness Type: Enhancement Most issues will probably ask for additions or changes. labels Apr 27, 2021
@azjezz azjezz linked an issue Apr 27, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Union for variadic types
3 participants