phptools-docs icon indicating copy to clipboard operation
phptools-docs copied to clipboard

generic issue

Open ging-dev opened this issue 1 year ago • 2 comments

<?php


/**
 * @template Tkey
 * @template TValue
 */
class Collection
{
    /**
     * @template TMap
     * @param callable(TValue, Tkey): TMap $callable
     * @return static<TKey, TMap>
     */
    public function map($callable)
    {

    }
}

/**
 * @template TKey
 * @template TValue
 * 
 * @param iterable<TKey, TValue>|array<TValue> $iter
 * @return Collection<TKey, TValue>
 */
function collection($iter)
{

}

$c = collection([1, 2, 3]);
$c1 = $c->map(function ($value, $key): string {
    // $value should be int
    return 'string';
}); // $c1 should be Collection<TKey, string> instead  Collection<TKey, int>

ging-dev avatar May 07 '24 16:05 ging-dev

Thank you!

We have ignored the generic arguments for static in this case. The $value type may be a bit more difficult, working on it.

  • [x] bind TMap in static<TKey, TMap>
  • [x] resolve lambda's parameter $value from the calling method generic parameter description

jakubmisek avatar May 10 '24 13:05 jakubmisek

A small bug related to hover differences in vendor: image

ging-dev avatar May 11 '24 04:05 ging-dev

Implemented inferring for lambda parameters + inlay type hint:

$c->map(function (/*int*/ $value, $key): string {
    // $value is int
});

vsc-lambda-params-inlays

It will be in the next update.

jakubmisek avatar May 28 '24 10:05 jakubmisek

Closing the issue.

The incorrect (double) hover will be fixed separately.

jakubmisek avatar May 28 '24 10:05 jakubmisek