Git Product home page Git Product logo

Comments (3)

weirdan avatar weirdan commented on September 26, 2024 2

Note to future me: callable(T):void should be valid in this context, but callable():T shouldn't.

from psalm.

psalm-github-bot avatar psalm-github-bot commented on September 26, 2024

I found these snippets:

https://psalm.dev/r/2eb66f01fe
<?php

/**
 * @template-covariant T
 */
class Example
{
    /**
     * @param T $value
     */
    function __construct(private mixed $value)
    {}
    
    /**
     * @param callable(T):void $callback 
     */
    function call(callable $callback): void
    {
    	$callback($this->value);
    }
}
Psalm output (using commit ef3b018):

ERROR: InvalidTemplateParam - 15:15 - Template param T of Example is marked covariant and cannot be used here
https://psalm.dev/r/3ebad94817
<?php

/**
 * @template-covariant T
 */
class Example
{
    /**
     * @param T $value
     */
    function __construct(private mixed $value)
    {}
    
    /**
     * @psalm-suppress InvalidTemplateParam
     * @param callable(T):void $callback 
     */
    function call(callable $callback): void
    {
    	$callback($this->value);
    }
}

class Animal {}
class Cat extends Animal {}
class Dog extends Animal {}

/** @psalm-suppress UnusedClosureParam */
function exampleWithObjects(): void
{
    $example = new Example(new Cat());
    
    $example->call(function (Animal $animal) {});
    $example->call(function (Cat $animal) {});
    $example->call(function (Dog $animal) {}); // OK (expected error)
}

/**
 * @param Cat[] $cats
 * @param callable(Animal[]):void $takesAnimals
 * @param callable(Cat[]):void $takesCats
 * @param callable(Dog[]):void $takesDogs
 */
function exampleWithArrays(
    array $cats,
    callable $takesAnimals,
    callable $takesCats,
    callable $takesDogs,
): void {
    $example = new Example($cats);
    
    $example->call($takesAnimals);
    $example->call($takesCats);
    $example->call($takesDogs); // OK (expected error)
}


/**
 * @param \ArrayObject<int, Cat> $cats
 * @param callable(\ArrayObject<int, Animal>):void $takesAnimals
 * @param callable(\ArrayObject<int, Cat>):void $takesCats
 * @param callable(\ArrayObject<int, Dog>):void $takesDogs
 */
function exampleWithArrayObjects(
    \ArrayObject $cats,
    callable $takesAnimals,
    callable $takesCats,
    callable $takesDogs,
): void {
    $example = new Example($cats);
    
    $example->call($takesAnimals); // OK (expected error)
    $example->call($takesCats);
    $example->call($takesDogs);  // OK (expected error)
}
Psalm output (using commit ef3b018):

ERROR: InvalidArgument - 35:20 - Argument 1 of Example::call expects callable(Cat):void, but pure-Closure(Dog):void provided

ERROR: InvalidArgument - 54:20 - Argument 1 of Example::call expects callable(array<array-key, Cat>):void, but callable(array<array-key, Dog>):void provided

ERROR: InvalidArgument - 72:20 - Argument 1 of Example::call expects callable(ArrayObject<int, Cat>):void, but callable(ArrayObject<int, Animal>):void provided

ERROR: InvalidArgument - 74:20 - Argument 1 of Example::call expects callable(ArrayObject<int, Cat>):void, but callable(ArrayObject<int, Dog>):void provided

from psalm.

vkurdin avatar vkurdin commented on September 26, 2024

It would be awesome if Psalm supported this Scala machinery on callables and variance: https://scastie.scala-lang.org/ZbzouFbvRHGPSwwB8OrNoA
+A stands for @template-covariant A
-A is @template-contravariant A
Unit is void
A => Boolean is callable(A): bool
(A => Boolean) => Boolean is callable(callable(A): bool): bool

from psalm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.