# [RFC][DISCUSSION] Object type hint, now with added variance

- Thread: https://externals.io/message/97135
- Original archive: https://news-web.php.net/php.internals/97135
- Started by Dan Ackroyd on 2016-11-23T00:52:02+00:00
- Messages: 21
- RFC: Object typehint (Implemented (PHP 7.2)) — https://externals.io/rfc/object-typehint.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #97135 — Dan Ackroyd — 2016-11-23T00:52:02+00:00

URL: https://externals.io/message/97135#97135

Hi,

This is the reintroduction of the Object Type RFC for discussion.

There was previously strong feedback from people who would prefer that
the inheritance checks for methods that use object types should be
co/contravariant. This has been added to the RFC.

https://wiki.php.net/rfc/object-typehint

cheers
Dan
Ack

---

## #97136 — Levi Morrison — 2016-11-23T01:25:03+00:00

URL: https://externals.io/message/97135#97136 · In reply to: #97135

> Hi,
>
> This is the reintroduction of the Object Type RFC for discussion.
>
> There was previously strong feedback from people who would prefer that
> the inheritance checks for methods that use object types should be
> co/contravariant. This has been added to the RFC.
>
> https://wiki.php.net/rfc/object-typehint
>
> cheers
> Dan
> Ack
>
> --
> 
> 

I strongly oppose this additional variance on the grounds that it
assumes that all types that are not statically known to the engine are
objects. This is a restrictive future compatibility concern. For
instance enumerations (or enums) are one possible type I can see us
adding that may not be objects. Additionally if we ever add named
aliases for types and union types then those types are not necessarily
objects either. Giving up these options for the sake of object
variance is definitely not prudent in my opinion.

---

## #97137 — Marco Pivetta — 2016-11-23T01:42:32+00:00

URL: https://externals.io/message/97135#97137 · In reply to: #97135

This looks awesome to me: nicely cleaned up! 👍



> Hi,
>
> This is the reintroduction of the Object Type RFC for discussion.
>
> There was previously strong feedback from people who would prefer that
> the inheritance checks for methods that use object types should be
> co/contravariant. This has been added to the RFC.
>
> https://wiki.php.net/rfc/object-typehint
>
> cheers
> Dan
> Ack

---

## #97138 — Joe Watkins — 2016-11-23T05:47:44+00:00

URL: https://externals.io/message/97135#97138 · In reply to: #97137

Morning Dan,

I took the liberty of removing the old voting doodle, updating the status
on the RFC, and moving to appropriate place on /rfc index.

Also, I corrected a typo.

LGTM.

Cheers
Joe



> This looks awesome to me: nicely cleaned up! 👍
>
> 
>
> > […]

---

## #97142 — michal@brzuchalski.com — 2016-11-23T10:38:24+00:00

URL: https://externals.io/message/97135#97142 · In reply to: #97138

Hi All,

I've added link to 3v4l.org at the bootom of RFC document so it is possible
to execute code online on compiled patch at https://3v4l.org/ results are
presented on tab "RFC Branches"

Cheers,
Michał

2016-11-23 6:47 GMT+01:00 Joe Watkins <pthreads@pthreads.org>:

> Morning Dan,
>
> I took the liberty of removing the old voting doodle, updating the status
> on the RFC, and moving to appropriate place on /rfc index.
>
> Also, I corrected a typo.
>
> LGTM.
>
> Cheers
> Joe
>
> 
>
> > […]
>



-- 
regards / pozdrawiam,
--
Michał Brzuchalski
about.me/brzuchal
brzuchalski.com

---

## #97144 — Marc Bennewitz — 2016-11-23T11:07:13+00:00

URL: https://externals.io/message/97135#97144 · In reply to: #97135

Am 23.11.2016 um 01:52 schrieb Dan Ackroyd:
> Hi,
>
> This is the reintroduction of the Object Type RFC for discussion.
>
> There was previously strong feedback from people who would prefer that
> the inheritance checks for methods that use object types should be
> co/contravariant. This has been added to the RFC.
>
> https://wiki.php.net/rfc/object-typehint

I like this RFC :)

And I have a question where I'm not sure if that should be catched by 
this RFC, too:

If I declare a return type of "object" and extends that to return 
"static" it doesn't compile because "static" is not a valid return type yet.

https://3v4l.org/lp3AB/rfc#version=rfc-typed-properties

class Test {
     public static function factory() : object {
         return new static();
     }
}

class Test2 extends Test {
     public static function factory() : static {
         return parent::factory();
     }
}


>
> cheers
> Dan
> Ack

---

## #97145 — David Rodrigues — 2016-11-23T11:25:43+00:00

URL: https://externals.io/message/97135#97145 · In reply to: #97144

Marc Bennewitz wrote:
> If I declare a return type of "object" and extends that to return "static" it doesn't compile because "static" is not a valid return type yet.
> https://3v4l.org/lp3AB/rfc#version=rfc-typed-properties

I think that it is need too, because it will avoid reference the class
by name when you need return it - but not is a biiig problem too.

2016-11-23 9:07 GMT-02:00 Marc Bennewitz <dev@mabe.berlin>:
>
>
> Am 23.11.2016 um 01:52 schrieb Dan Ackroyd:
> > […]
>
>
> I like this RFC :)
>
> And I have a question where I'm not sure if that should be catched by this
> RFC, too:
>
> If I declare a return type of "object" and extends that to return "static"
> it doesn't compile because "static" is not a valid return type yet.
>
> https://3v4l.org/lp3AB/rfc#version=rfc-typed-properties
>
> class Test {
>     public static function factory() : object {
>         return new static();
>     }
> }
>
> class Test2 extends Test {
>     public static function factory() : static {
>         return parent::factory();
>
>     }
> }
>
>
> > […]
>
> --
> 
> 
>



-- 
David Rodrigues

---

## #97153 — Dan Ackroyd — 2016-11-24T08:12:40+00:00

URL: https://externals.io/message/97135#97153 · In reply to: #97144

>
> If I declare a return type of "object" and extends that to return "static"
> it doesn't compile because "static" is not a valid return type yet.
>

Hi Marc,

That should be part of a separate RFC, that is focused on making
static a valid return type.

cheers
Dan

---

## #97154 — Dan Ackroyd — 2016-11-24T08:58:08+00:00

URL: https://externals.io/message/97135#97154 · In reply to: #97136

Hi Levi,


>
> it assumes that all types that are not statically known to the engine are
> objects. This is a restrictive future compatibility concern.

Any RFC and/or patch can only deal with PHP as it currently is.

> Giving up these options for the sake of object
> variance is definitely not prudent in my opinion.

We would not necessarily be giving them up. We are just adding a small
amount of complexity that would need to be handled by future RFCs.

Which is true of all RFCs.

> For instance enumerations (or enums) are one possible type I can see us
> adding that may not be objects.

I very much look forward to the RFC for enums, and have for some time.
When are you thinking of submitting it for discussion and voting?

cheers
Dan
Ack

---

## #97155 — Niklas Keller — 2016-11-24T09:35:48+00:00

URL: https://externals.io/message/97135#97155 · In reply to: #97154

2016-11-24 9:58 GMT+01:00 Dan Ackroyd <danack@basereality.com>:

> Hi Levi,
>
> 
> > […]
>
> Any RFC and/or patch can only deal with PHP as it currently is.


No. We had a similar discussion for the context sensitive lexer RFC and
didn't allow some things to make future features easier.


> > […]
>
> We would not necessarily be giving them up. We are just adding a small
> amount of complexity that would need to be handled by future RFCs.
>

It's not a small amount of complexity, otherwise we'd already have contra /
covariance.

Regards, Niklas


> Which is true of all RFCs.
>
> > […]
>
> I very much look forward to the RFC for enums, and have for some time.
> When are you thinking of submitting it for discussion and voting?
>
> cheers
> Dan
> Ack

---

## #98290 — Dan Ackroyd — 2017-02-14T18:30:00+00:00

URL: https://externals.io/message/97135#98290 · In reply to: #97155

(resurrecting the thread....)



> It's not a small amount of complexity,

So that other people can understand the tradeoff, please could you
detail the complexity as you understand it?

> otherwise we'd already have contra / covariance.

My understanding is that we already allow this where possible. For
example your type-widening RFC
https://wiki.php.net/rfc/parameter-no-type-variance

What is the problem with allowing it here?

cheers
Dan

---

## #98291 — Dan Ackroyd — 2017-02-14T18:55:55+00:00

URL: https://externals.io/message/97135#98291 · In reply to: #97154

Hi Levi,


> Hi Levi,
>
> 
>
> > […]
>
> I very much look forward to the RFC for enums, and have for some time.
> When are you thinking of submitting it for discussion and voting?

I can understand your concerns. At the same time, I hope you can
understand that people won't want to put off introducing features that
they want in the meantime.

I think if you were able to give a timeline for when an Enum RFC could
be introduced, before the PHP 7.2 release, we could talk about whether
it would be right to hold off on this RFC for now.

Otherwise I suggest Michał updates the voting options, and we open the
voting in a week or so.

cheers
Dan

---

## #98293 — Levi Morrison — 2017-02-14T21:14:43+00:00

URL: https://externals.io/message/97135#98293 · In reply to: #98291

> Hi Levi,
>
> 
> > […]
>
> I can understand your concerns. At the same time, I hope you can
> understand that people won't want to put off introducing features that
> they want in the meantime.
>
> I think if you were able to give a timeline for when an Enum RFC could
> be introduced, before the PHP 7.2 release, we could talk about whether
> it would be right to hold off on this RFC for now.
>
> Otherwise I suggest Michał updates the voting options, and we open the
> voting in a week or so.
>
> cheers
> Dan

Let me clarify that I'm not against this object variance; there is
nothing intrinsic about object variance that prevents enums (or
typedefs/type aliases). Rather it's *how* it is achieving that
variance that is an issue. This is something that should ship along
with full covariance/contravariance and not be a special case which
may unintended side-effects later (such as preventing enums without
breaking BC). Does that make sense?

---

## #98294 — Levi Morrison — 2017-02-14T21:20:02+00:00

URL: https://externals.io/message/97135#98294 · In reply to: #98293

> 
> > […]
>
> Let me clarify that I'm not against this object variance; there is
> nothing intrinsic about object variance that prevents enums (or
> typedefs/type aliases). Rather it's *how* it is achieving that
> variance that is an issue. This is something that should ship along
> with full covariance/contravariance and not be a special case which
> may unintended side-effects later (such as preventing enums without
> breaking BC). Does that make sense?

Here's a concrete compatibility concern. Let's assume we have the
following code:

    interface Factory {
        function make(...$args): object;
    }

    class FooFactory {
        function make(...$args): Foo;
    }

Currently the engine would not have to know anything about Foo to know
if it is a sub-type of object. It would not need to require it to even
be defined.

However, if at some point in the future we add non-object types it may
now error or autoload.

The way to avoid the compatibility concern is to implement this
feature the same way we wold implement full covariance, which would
similarly need to know about the types involved.

Does that make more sense as to why the assumption that unknown types
are object types is an issue?

---

## #98295 — Levi Morrison — 2017-02-14T21:20:52+00:00

URL: https://externals.io/message/97135#98295 · In reply to: #98294

(Oops, I forgot to inherit the interface:

     interface Factory {
         function make(...$args): object;
     }

     class FooFactory implements Factory {
         function make(...$args): Foo;
     }

sorry for the noise)

---

## #98297 — Marco Pivetta — 2017-02-15T07:52:37+00:00

URL: https://externals.io/message/97135#98297 · In reply to: #98294

Hi Levi,





> On Tue, Feb 14, 2017 at 11:55 AM, Dan Ackroyd <danack@basereality.com>
wrote:
> > […]
>
> Let me clarify that I'm not against this object variance; there is
> nothing intrinsic about object variance that prevents enums (or
> typedefs/type aliases). Rather it's *how* it is achieving that
> variance that is an issue. This is something that should ship along
> with full covariance/contravariance and not be a special case which
> may unintended side-effects later (such as preventing enums without
> breaking BC). Does that make sense?

Here's a concrete compatibility concern. Let's assume we have the
following code:

    interface Factory {
        function make(...$args): object;
    }

    class FooFactory {
        function make(...$args): Foo;
    }

Currently the engine would not have to know anything about Foo to know
if it is a sub-type of object. It would not need to require it to even
be defined.

However, if at some point in the future we add non-object types it may
now error or autoload.



Since the engine is clueless about types until autoloading happens, this is
easily solvable by providing a marker syntax for non-object hints. For
instance `function foo() : enym:MyEnum {}`

The current (and valid) assumption by both the engine and userland
reflectors is that anything after the `:` in a return type declaration is
one of:

 * bool
 * array
 * int
 * float
 * string
 * callable
 * void
 * $classOrInterfaceName

Regardless of this RFC, and considered that `object` is an already reserved
keyword, you can't use the same syntax for enum types anyway, since you're
going to break a crapton of userland code.


The way to avoid the compatibility concern is to implement this
feature the same way we wold implement full covariance, which would
similarly need to know about the types involved.

Does that make more sense as to why the assumption that unknown types
are object types is an issue?

--




Given inline comments above, can we archive the enum discussion?

---

## #98300 — Andrea Faulds — 2017-02-15T15:34:25+00:00

URL: https://externals.io/message/97135#98300 · In reply to: #98297

Hi Marco,

Marco Pivetta wrote:
>
> Since the engine is clueless about types until autoloading happens, this is
> easily solvable by providing a marker syntax for non-object hints. For
> instance `function foo() : enym:MyEnum {}`

If we're going to do prefixes for non-classes, we might as well copy C 
outright: `enum MyEnum`.

Thanks.
-- 
Andrea Faulds
https://ajf.me/

---

## #98305 — Rasmus Schultz — 2017-02-15T19:53:51+00:00

URL: https://externals.io/message/97135#98305 · In reply to: #98300

In the light of this, maybe enums (assuming this feature comes first)
should be implemented as immutable objects rather than values?

Type-checking for enums would be important, so in PHP terms, an enum
"instance" would likely be closer to an object than a value in nature
anyway.

The way I look at it, this feature doesn't prevent enums from happening, it
just changes the design parameters.

Just a thought.




> Hi Marco,
>
> Marco Pivetta wrote:
>
> > […]
>
> If we're going to do prefixes for non-classes, we might as well copy C
> outright: `enum MyEnum`.
>
> Thanks.
> --
> Andrea Faulds
> https://ajf.me/

---

## #98306 — michal@brzuchalski.com — 2017-02-15T20:25:10+00:00

URL: https://externals.io/message/97135#98306 · In reply to: #98305

Assumming they would be closer to immutable objects they may have some
usefull... I was gonna say methods but the term doesn't fit to enums.
But using one of my favourite userland implementation such as `esky\enum`
package there are ways to create enumeration from value or from name.
Those methods are quite usefull when converting user data passed in request
from string to enumeration.
Given the example:

enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}

When using passed from request data proper enumeration could be restored
for eg.

$_GET['day'] = 'MONDAY';
$day = Day::fromName($_GET['day']); // Day.MONDAY

or by value

$day = Day::fromValue(0); // Day.MONDAY

Think of it as possible way of restoring from primitive types because then
enums would really be closer to classes than values.
And also then could even utilise `object

2017-02-15 20:53 GMT+01:00 Rasmus Schultz <rasmus@mindplay.dk>:

> In the light of this, maybe enums (assuming this feature comes first)
> should be implemented as immutable objects rather than values?
>
> Type-checking for enums would be important, so in PHP terms, an enum
> "instance" would likely be closer to an object than a value in nature
> anyway.
>
> The way I look at it, this feature doesn't prevent enums from happening, it
> just changes the design parameters.
>
> Just a thought.
>
>
> 
>
> > […]
>



-- 
regards / pozdrawiam,
--
Michał Brzuchalski
about.me/brzuchal
brzuchalski.com

---

## #98307 — michal@brzuchalski.com — 2017-02-15T20:26:32+00:00

URL: https://externals.io/message/97135#98307 · In reply to: #98306

2017-02-15 21:25 GMT+01:00 Michał Brzuchalski <michal@brzuchalski.com>:

> Assumming they would be closer to immutable objects they may have some
> usefull... I was gonna say methods but the term doesn't fit to enums.
> But using one of my favourite userland implementation such as `esky\enum`
> package there are ways to create enumeration from value or from name.
> Those methods are quite usefull when converting user data passed in
> request from string to enumeration.
> Given the example:
>
> enum Day {
>     MONDAY,
>     TUESDAY,
>     WEDNESDAY,
>     THURSDAY,
>     FRIDAY,
>     SATURDAY,
>     SUNDAY
> }
>
> When using passed from request data proper enumeration could be restored
> for eg.
>
> $_GET['day'] = 'MONDAY';
> $day = Day::fromName($_GET['day']); // Day.MONDAY
>
> or by value
>
> $day = Day::fromValue(0); // Day.MONDAY
>
> Think of it as possible way of restoring from primitive types because then
> enums would really be closer to classes than values.
> And also then could even utilise `object
>

typehint so it'll accept enums also.
(Sorry for second mail - unintended Ctrl+Enter)


>
> 2017-02-15 20:53 GMT+01:00 Rasmus Schultz <rasmus@mindplay.dk>:
>
> > […]
>
>
>
> --
> regards / pozdrawiam,
> --
> Michał Brzuchalski
> about.me/brzuchal
> brzuchalski.com
>



-- 
regards / pozdrawiam,
--
Michał Brzuchalski
about.me/brzuchal
brzuchalski.com

---

## #98309 — Nikita Popov — 2017-02-15T21:13:00+00:00

URL: https://externals.io/message/97135#98309 · In reply to: #98294

> 
> > […]
> wrote:
> > […]
> wrote:
> > […]
> us
> > […]
>
> Here's a concrete compatibility concern. Let's assume we have the
> following code:
>
>     interface Factory {
>         function make(...$args): object;
>     }
>
>     class FooFactory {
>         function make(...$args): Foo;
>     }
>
> Currently the engine would not have to know anything about Foo to know
> if it is a sub-type of object. It would not need to require it to even
> be defined.
>
> However, if at some point in the future we add non-object types it may
> now error or autoload.
>
> The way to avoid the compatibility concern is to implement this
> feature the same way we wold implement full covariance, which would
> similarly need to know about the types involved.
>
> Does that make more sense as to why the assumption that unknown types
> are object types is an issue?
>

Thanks, this argument is convincing.

Overall, I think the utility of the object typehint is very limited. If
introducing an object typehint now will pose issues for an enum
implementation later (which I consider significantly more important), I
would prefer to defer its introduction until a later time.

People have pointed out that there will still be ways to implement enums
without breaking BC, either by prefixing enum type annotations with "enum",
or by making enum values objects. These suggestions may have merit, but
they should be evaluated on their *own* merit when the proposal comes up.
We should avoid being forced into a corner where we have to chose a
specific implementation due to BC concerns.

Nikita
