-
Notifications
You must be signed in to change notification settings - Fork 1
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 explicit psalm template to enforce type inference and remove psalm baseline #1
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Emanuele Menon <[email protected]>
Decoders::make( | ||
/** @return Validation<self> */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the part that I do not like, you have to fix this manually every time.
Maybe this is due to Validation::failure
that says @psalm-return ValidationFailures<T>
instead of @psalm-return ValidationFailures<never>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree on disliking that part.
Looking further, what i see is that the codomain of Decoders::make
is a Validation<A>
where A
is inferred by the "sum" of the codomain of the Validation::success
and Validation::failure
.
I guess that the blame is on the Validation::failure
sign that has no clue to infer the right type of what is failed to validates here.
A possibile suggested solution is to tight the type of A
inferred from the failure method making it explicit.
// Validation.php
...
/**
* @psalm-template T
* @psalm-param mixed $value
* @psalm-param Context $context
* @psalm-param class-string<T> $of
* @psalm-param string|null $message
* @psalm-return ValidationFailures<T>
*
* @param mixed $value
*/
public static function failureT(string $of, $value, Context $context, ?string $message = null): self
{
return self::failures(
[new VError($value, $context, $message)]
);
}
...
However, this requires you to make changes to the php-codec
library.
I have no clue on how to solve it purely on client side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO that's not possibile, even on the lib side: A
could be not existing in a case of failure, especially in our case: the VO shouldn't exist in an invalid state, but in that way you're forcing me to create it as invalid.
Maybe you could force the failure to contain a class-string<A>
, but it seems clunky.
@ilario-pierbattista WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, and I am aware of this issue.
My goal is to have Validation<T>
being isomorphic to a generic Either<VError[]. T>
.
Maybe I could find a better implementation in other function libraries, such as https://github.com/marcosh/lamphpda .
Btw, class-string<T>
won't work in all cases, since it needs to deal also with non-object values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Signed-off-by: Emanuele Menon [email protected]