haxe icon indicating copy to clipboard operation
haxe copied to clipboard

[php] return type declarations

Open PaulPatat opened this issue 1 year ago • 2 comments

If I have this extern for a CakePHP class

package cake.orm;

import php.NativeArray;

@:native("Cake\\ORM\\Table")
extern class Table {
        function initialize(config:NativeArray):Void;
}

And this subclass

package app.model.table;

import cake.orm.Table;

class CookiesTable extends Table
{
        override function initialize(config):Void
        {
        }

}

Then this php is emitted


namespace app\model\table;
.....
       /**
         * @param array $config
         *
         * @return void
         */
        public function initialize ($config) {

But initialize() has a return type declaration from CakePHP 4 onward so this doesn't work too well:


class Table {
    public function initialize(array $config): void

It would be nice if extern methods could be annotated as having a return type so the compiler would emit overrides with the correct type.

PaulPatat avatar Sep 03 '24 08:09 PaulPatat

So the problem is that the compiler doesn't emit the : void explicitly?

Simn avatar Oct 16 '24 07:10 Simn

Yes. You'd subclass framework classes and override some methods so it has to obey the covariance and contravariance rules. Of course it's not just Void, for example you'd also override buildRules():

  public function buildRules(RulesChecker $rules): RulesChecker

PaulPatat avatar Oct 18 '24 05:10 PaulPatat