-
Notifications
You must be signed in to change notification settings - Fork 86
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
Immutable classes with pure constructor and same constant args as single object #754
Comments
I strongly disagree about this change. Two independent calls of the constructor MUST return two independent instances, but suggested feature just breaks this rule and affects runtime. In fact, this issue introduces some kind of compile-time singleton, but in a very implicit way (counterintuitive, in fact). Two identical objects, even immutable ones, are still not the same object in general case, so singletonization must be done in user code explicitly. So this in not an optimization, but a breaking change. |
Thank you for the comment. Naturally, it's internal compiler change and it should not affect on current code bases because all constructors are not Could you give an example of breaking cases, please? |
I missed the fact that By the way, such behaviour can't be achieved in pure PHP, so there will be inconsistency between PHP and KPHP. |
Not really. Actually, this annotation is implemented, but behavior is not guaranteed. There's going to be another issue about dealing with For now, the most motivated example is |
Do you mean it's gonna be kind of workaround while there's no real |
We decided to change a little bit initial idea. For now, we implemented a part of https://www.php.net/releases/8.1/en.php#new_in_initializers, namely it's possible to use objects in constant definition. See #814. |
In case of
we want
A
with parametersCONST_ONE, CONST_TWO
to be called once and transpiled as global variable that is initialized only oncenew A(CONST_ONE, CONST_TWO)
to be replaced with such a variableIt would allow use some constructions such as
with the following restrictions on
class A
:@kphp-immutable-class
@kphp-pure-function
CONST_ONE, CONST_TWO
are constant expressionsIt would possibly improve performance and definitely help to implement
enums
in a natural way.The text was updated successfully, but these errors were encountered: