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
Variance misunderstanding [Question] #5252
Comments
I found these snippets: https://psalm.dev/r/9881859823<?php
/**
* @template-covariant A
* @template-covariant B
* @psalm-immutable
*/
final class Type {
/**
* @template C
*
* @param Type<B, C> $type
* @return Type<A, C>
*/
public function compose(Type $type): Type
{
throw new \RuntimeException('???');
}
}
/** @var Type<mixed, positive-int> $fromMixedToPositiveInt */
$_ = null;
/** @var Type<int, stdClass> $fromIntToStdClass */
$_ = null;
$fromMixedToPositiveInt->compose($fromIntToStdClass);
|
|
@muglug It seems I asked an imprecise question and spent your time. Sorry. I tried to implement the simple idea of function composition. But for types.
Look to this snippet: Please, tell me. Why composition can be implemented with Closure (i treat it as workaround) |
I found these snippets: https://psalm.dev/r/07db7bc8f9<?php
/**
* @template A
* @template-covariant B
* @psalm-immutable
*/
final class Type
{
/**
* @template C
*
* @param Closure(B): C $bc
* @return Type<A, C>
*/
public function composeWithClosure(Closure $bc): Type
{
throw new RuntimeException('???');
}
/**
* @template C
*
* @param Type<B, C> $bc
* @return Type<A, C>
*/
public function composeWithFunc(Type $bc): Type
{
throw new RuntimeException('???');
}
}
/** @var Type<mixed, positive-int> $func1 */
$func1 = null;
/** @var Type<int, string> $func2 */
$func2 = null;
$closure =
/**
* @param int $_
* @return string
*/
fn(int $_) => (string) $_;
// $closure AND $func2 semantically the same
// hacky-composition possible with Closure
$work = $func1->composeWithClosure($closure);
// But regular composition Func<A, B> and Func<B, C> impossible
$doesNotWork = $func1->composeWithFunc($func2);
|
Sorry. Snippet above may be confused. Use that https://psalm.dev/r/2e6727b92a |
I found these snippets: https://psalm.dev/r/2e6727b92a<?php
/**
* @template A
* @template-covariant B
* @psalm-immutable
*/
final class Type
{
/**
* @template C
*
* @param Closure(B): C $bc
* @return Type<A, C>
*/
public function composeWithClosure(Closure $bc): Type
{
throw new RuntimeException('???');
}
/**
* @template C
*
* @param Type<B, C> $bc
* @return Type<A, C>
*/
public function composeWithType(Type $bc): Type
{
throw new RuntimeException('???');
}
}
/** @var Type<mixed, positive-int> $func1 */
$func1 = null;
/** @var Type<int, string> $func2 */
$func2 = null;
$closure =
/**
* @param int $_
* @return string
*/
fn(int $_) => (string) $_;
// $closure AND $func2 semantically the same
// hacky-composition possible with Closure
$work = $func1->composeWithClosure($closure);
// But regular composition Type<A, B> and Type<B, C> impossible
$doesNotWork = $func1->composeWithType($func2);
|
Ah, because I don't think they're commonly-used. Feel free to create a PR! |
Could you please give an explanation why I cannot do this?
https://psalm.dev/r/9881859823
The text was updated successfully, but these errors were encountered: