Skip to content

[RFC] Change ResultInterface<T> to ResultInterface<T, E> #440

Open
@devnix

Description

@devnix

I've been chatting with @veewee on Discord about adding a template to ResultInterface to return what kind of error it could contain.

I'm not sure if this could be considered a breaking change, as it only affects static analysis and can be easily ignored, baselined, or fixed in a moment by going to each error and changing every @return ResultInterface<Foo> to @return ResultInterface<Foo, Throwable> if you don't feel like studying the specific throwables you want to return from there.

Another option apart from waiting for a new major version or just letting static analysis break is to wait for some default generic type syntax, see:

This could allow us to have a syntax that would behave the same way but without having any new errors:

/**
 * @template T
 * @template E of Throwable = Throwable
 */
interface ResultInterface {}

If we are OK with baselining errors (regardless of whether the change is in a minor or major version), the following syntax would infer the template with Throwable as the default value:

/**
 * @template T
 * @template E of Throwable
 */
interface ResultInterface {}

Psalm would only throw a MissingTemplateParalm. At the same time, PHPStan would report two errors. Still, the return would be implicitly inferred from ResultInterface<int> to ResultInterface<Throwable> in the not_documented() case on both major static analysis tools.


Apart from this, there is another feature that would be nice to have in the static analysis, and that is to be able to infer the type of @throws with a generic, so we could type this:

/**
 * @template T
 * @template E of Throwable = Throwable
 */
interface ResultInterface {
    /**
     * @return T
     * 
     * @throws E
     */
    public function getResult();
}

Metadata

Metadata

Assignees

Labels

Priority: LowThis issue can probably be picked up by anyone looking to contribute to the project, as an entry fixType: EnhancementMost issues will probably ask for additions or changes.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions