-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How are constructor types inferred? #4
Comments
Well, from a static analysis point of view, there is two possibilities:
|
Great question. My initial response is that the type must be explicitly stated: e.g. /** @var ValueHolder<int> $intHolder */
$intHolder = new ValueHolder($value) ; This maps to what would happen in Java (based on my Java knowledge from 5 years ago!) ... intHolder = new ValueHolder<int>(); @orklah raises an interesting point when inferring the type. Is Either way the ambiguity suggests adds weight the argument that the type should be explicitly stated. Thoughts? |
The description in https://github.com/DaveLiddament/php-generics-standard#constructor is a bit unclear to me. If you are passing a literal integer value, it's obvious that
T = int
, but what if the value is dynamic?In this case,
$foo
can beint
ornull
at runtime. However, I would assume that the actual inferred type isT = ?int
. Is that correct?If it is, then what about this variant?
Here,
$foo
is known to beint
, but only through flow-sensitive type analysis. WouldT = int
here?The text was updated successfully, but these errors were encountered: