Dear Internals,
I finally have a working implementation for return types RFC1 built
on top of master. There are a few notes in the PR2 about the
implementation. I invite you all to review the PR and provide
feedback.
This means that I will soon move the return types RFC to voting phase.
If you have not yet had time to review the RFC recently I invite you
to do so now.
The RFC has been slightly altered since the last discussion:
- The RFC now targets PHP 7 (previously PHP 5.7).
- There is a new section about disallowing return types on certain methods4.
- The design and accompanying section of reflection3 has been
rewritten entirely.
Regardless of the result of the RFC, I want to thank the many people
who have been helpful to me as I have learned php-src and iterated
over this RFC.
The RFC is very consistent.
It proposes only a part of the desired type-hinting features, but this part
is not questionable for me.
+1
The implementation has some minor issues.
Levi, if you won't object, I would like to play with implementation a bit,
right before commit (if it accepted).
Thanks. Dmitry.
On Thu, Oct 16, 2014 at 8:39 AM, Levi Morrison levi_morrison@byu.edu
wrote:
Dear Internals,
I finally have a working implementation for return types RFC1 built
on top of master. There are a few notes in the PR2 about the
implementation. I invite you all to review the PR and provide
feedback.This means that I will soon move the return types RFC to voting phase.
If you have not yet had time to review the RFC recently I invite you
to do so now.The RFC has been slightly altered since the last discussion:
- The RFC now targets PHP 7 (previously PHP 5.7).
- There is a new section about disallowing return types on certain
methods4.- The design and accompanying section of reflection3 has been
rewritten entirely.Regardless of the result of the RFC, I want to thank the many people
who have been helpful to me as I have learned php-src and iterated
over this RFC.
Dmitry
The RFC is very consistent.
It proposes only a part of the desired type-hinting features, but this part
is not questionable for me.
May I enquire what it misses out according to your ideal desired
feature set? Just so that I/everyone is clear on what the limitations
and potential areas for future expansion are, and to ensure nothing
has been missed that could be included in this change without being
too contentious.
Apart from distinct features (e.g. scalar types) I'm not sure what is
missing from the proposal, so I'd like to know what I'm missing.
Also, just for the record, I am +1 on the RFC as it currently stands.
Thanks, Chris
Hi Chris,
I meant scalar type hinting, nullable type hinting, may be even mixed type
hinting...
But, lets go by small steps to get all this finally accepted.
Thanks. Dmitry.
Dmitry
The RFC is very consistent.
It proposes only a part of the desired type-hinting features, but this
part
is not questionable for me.May I enquire what it misses out according to your ideal desired
feature set? Just so that I/everyone is clear on what the limitations
and potential areas for future expansion are, and to ensure nothing
has been missed that could be included in this change without being
too contentious.Apart from distinct features (e.g. scalar types) I'm not sure what is
missing from the proposal, so I'd like to know what I'm missing.Also, just for the record, I am +1 on the RFC as it currently stands.
Thanks, Chris
The RFC is very consistent.
It proposes only a part of the desired type-hinting features, but this part
is not questionable for me.+1
I second that emotion; +1.
On Thu, Oct 16, 2014 at 6:39 AM, Levi Morrison levi_morrison@byu.edu
wrote:
Dear Internals,
I finally have a working implementation for return types RFC[1] built
on top of master. There are a few notes in the PR[2] about the
implementation. I invite you all to review the PR and provide
feedback.This means that I will soon move the return types RFC to voting phase.
If you have not yet had time to review the RFC recently I invite you
to do so now.The RFC has been slightly altered since the last discussion:
- The RFC now targets PHP 7 (previously PHP 5.7).
- There is a new section about disallowing return types on certain
methods[4].- The design and accompanying section of reflection[3] has been
rewritten entirely.Regardless of the result of the RFC, I want to thank the many people
who have been helpful to me as I have learned php-src and iterated
over this RFC.
+1
What I like about this RFC:
- It only covers return types and nothing else (no nullable types, no
scalar types, etc), thus avoiding unnecessary controversy. - It uses postfix type notation, which is great for
grep
, is compatible
with Hack and avoids weird syntax with anonymous functions (and also with
method generics, should they be introduced in the future). - Sticking with covariant return types, even though it was tricky to
implement.
Nikita
- There is a new section about disallowing return types on certain methods[4].
Perhaps, along the same lines, we could error if you use a nonsensical type hint for magic methods?
For example, allow this:
class FooBar {
public function __debugInfo(): array {
return [’test’];
}
}
But not this:
class FooBar {
public function __debugInfo(): callable {
return [’test’];
}
}
It’s not strictly necessary, but it would catch out bugs at “compile-time” (i.e. script startup) rather than call-time.
Andrea Faulds
http://ajf.me/
I think the patch already does it (may be not for all magic methods).
Thanks. Dmitry.
- There is a new section about disallowing return types on certain
methods[4].Perhaps, along the same lines, we could error if you use a nonsensical
type hint for magic methods?For example, allow this:
class FooBar { public function __debugInfo(): array { return [’test’]; } }
But not this:
class FooBar { public function __debugInfo(): callable { return [’test’]; } }
It’s not strictly necessary, but it would catch out bugs at “compile-time”
(i.e. script startup) rather than call-time.Andrea Faulds
http://ajf.me/
I very much like this — though I would say it was dependent on the nullable
types RFC (like splat and variadics were codependent).
While I would like to see the introduction of a void type, I understand and
respect the limitations on the RFC.
However, one thing that I do think is missing, is the equivalent of Hacks
$this
return type. You have self
and parent
, but I think without a
static
equivalent you can break things:
class foo {
static public function instanceOf(): self {
return new static();
}
}
class bar extends foo { }
foo::instanceOf(); // new foo, this is fine, returns self
.
bar::instanceOf(); // new bar, no longer self
On Thu, Oct 16, 2014 at 12:39 AM, Levi Morrison levi_morrison@byu.edu
wrote:
Dear Internals,
I finally have a working implementation for return types RFC1 built
on top of master. There are a few notes in the PR2 about the
implementation. I invite you all to review the PR and provide
feedback.This means that I will soon move the return types RFC to voting phase.
If you have not yet had time to review the RFC recently I invite you
to do so now.The RFC has been slightly altered since the last discussion:
- The RFC now targets PHP 7 (previously PHP 5.7).
- There is a new section about disallowing return types on certain
methods4.- The design and accompanying section of reflection3 has been
rewritten entirely.Regardless of the result of the RFC, I want to thank the many people
who have been helpful to me as I have learned php-src and iterated
over this RFC.
I very much like this — though I would say it was dependent on the
nullable
types RFC (like splat and variadics were codependent).While I would like to see the introduction of a void type, I understand
and
respect the limitations on the RFC.However, one thing that I do think is missing, is the equivalent of Hacks
$this
return type. You haveself
andparent
, but I think without a
static
equivalent you can break things:class foo {
static public function instanceOf(): self {
return new static();
}
}class bar extends foo { }
foo::instanceOf(); // new foo, this is fine, returns
self
.
bar::instanceOf(); // new bar, no longerself
Hi,
bar is considered as instance of foo so shouldn't be any problem.
Otherwise it would break LSP
Hi Levi,
This RFC is pretty consistent, small and focused on a specific part of the
overall previously desired support.
I do think many more subsequent RFCs would come up after this one to expand
return typehint support, but as it stands right now is extremely good. Any
further inclusion would drastically delay possible acceptance of this RFC
and a possible withdrawn, since every piece left out are different
discussion points.
+1 so far.
[]s,
I very much like this — though I would say it was dependent on the
nullable
types RFC (like splat and variadics were codependent).While I would like to see the introduction of a void type, I understand
and
respect the limitations on the RFC.However, one thing that I do think is missing, is the equivalent of Hacks
$this
return type. You haveself
andparent
, but I think without a
static
equivalent you can break things:class foo {
static public function instanceOf(): self {
return new static();
}
}class bar extends foo { }
foo::instanceOf(); // new foo, this is fine, returns
self
.
bar::instanceOf(); // new bar, no longerself
Hi,
bar is considered as instance of foo so shouldn't be any problem.
Otherwise it would break LSP--
--
Guilherme Blanco
MSN: guilhermeblanco@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada
- The design and accompanying section of reflection3 has been
rewritten entirely.
I've some comments about the Reflection API addition/changes:
-
"Note that getReturnType will always return a ReflectionType
object, even if there is no return type declared."
That feels weird, TBH. Other parts of the Reflection-API don't do that,
they simply return false if I would call e.g. getParentClass() when
there's none.
-
"This API was designed so you could use ReflectionType::getKind()
in a switch statement to cover all cases, rather than be forced to use
an if/else structure that calls isArray(), isCallable, etc like the
ReflectionParameter API does."
I don't like the deviation from the existing Reflection API. I'm not
saying it's perfect, but I fear a "haystack,needle vs. needle,haystack"
thing here and would prefer the existing convention for is*() methods.
I wouldn't see a conflict it providing both as I see the usefulness.
- The new class is called "ReflectionType", shouldn't it be
"ReflectionReturnType" ?
- Other reflection classes, e.g. ReflectionParameter provides "missing
link" methods, specifically I'm missing:
public ReflectionClass getDeclaringClass ( void );
public ReflectionFunctionAbstract getDeclaringFunction ( void );
- I think, also like in ReflectionParameter, there should a direct
shortcut to the class, if provided (and not just the string name):
public ReflectionClass getClass ( void );
thanks,
- Markus
Markus
- The design and accompanying section of reflection3 has been
rewritten entirely.I've some comments about the Reflection API addition/changes:
"Note that getReturnType will always return a ReflectionType
object, even if there is no return type declared."That feels weird, TBH. Other parts of the Reflection-API don't do that,
they simply return false if I would call e.g. getParentClass() when
there's none.
That's a different situation though. A function always returns
something, even if no type was declared and there was no explicit
return statement. There's currently no concept of "void" in PHP in
this context.
"This API was designed so you could use ReflectionType::getKind()
in a switch statement to cover all cases, rather than be forced to use
an if/else structure that calls isArray(), isCallable, etc like the
ReflectionParameter API does."I don't like the deviation from the existing Reflection API. I'm not
saying it's perfect, but I fear a "haystack,needle vs. needle,haystack"
thing here and would prefer the existing convention for is*() methods.I wouldn't see a conflict it providing both as I see the usefulness.
I understand where you are coming from here, but if we are going to
take this approach we should provide both in the context of parameters
- i.e. transition to the new RelfectionType API everywhere - rather
than providing a leaky approach in the context of new features for the
sake of consistency. The ReflectionType was specifically designed to
be suitable for both parameters and return values because it
represents only the type, whereas the ReflectionParameter is only
suitable for, well, parameters, because it represents more.
I think that providing access to a ReflectionType instance from a
ReflectionParameter is going to be proposed before release, but it is
outside the scope of, and dependent on the acceptance of, this RFC.
- The new class is called "ReflectionType", shouldn't it be
"ReflectionReturnType" ?
No, see above.
- Other reflection classes, e.g. ReflectionParameter provides "missing
link" methods, specifically I'm missing:public ReflectionClass getDeclaringClass ( void );
public ReflectionFunctionAbstract getDeclaringFunction ( void );
This is a valid point, although remember a RelectionType represents a
type and not a return type declaration. I'm not sure if there's
value in adding a ReflectionReturnType layer between the type and the
function it is retrieved from (I suspect not, this level of
granularity seems unnecessary to me, you'd just pass the
ReflectionFunction around). A parameter is slightly different as it
has other properties as well as just a type declaration (name,
required, default value etc). The only other property a return type
could have is whether it is "nullable" - which is explicitly not part
of this RFC (see https://wiki.php.net/rfc/nullable_typehints) and
could be indicated using a bitmask with the getKind(), which I think
is part of the reason the RFC specifically states that the values of
the ReflectionType::IS_* constants should not be relied upon.
TL;DR I agree this is inconsistent, but I also don't think it matters
because I don't think anyone would need/use it.
I probably wouldn't complain if someone wanted to add it, though.
- I think, also like in ReflectionParameter, there should a direct
shortcut to the class, if provided (and not just the string name):public ReflectionClass getClass ( void );
Agreed (not 100% clear cut because there are special typehint cases
that do not have a corresponding class but I think this is somewhat
nitpicking)
Thanks, Chris
Hi Chris,
[...] in depth summary of future idea ReflectionType
Thanks a lot for the clarification and regarding the future expansion of
the use of ReflectionType I now better understand most points and why
they're currently are that way.
I don't think I've anything more of value to add (except thanks to the
hard work to all) but I guess I would prefer to see my point 5) being
addressed.
thanks,
- Markus
I have updated the section on Reflection to explain some of the design
decisions: https://wiki.php.net/rfc/returntypehinting#reflection;
hopefully that will help you and others to understand the rationale.
Please let me know if you have follow-up questions.