-
-
Notifications
You must be signed in to change notification settings - Fork 71
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
Allow disabling strict types. #235
base: master
Are you sure you want to change the base?
Conversation
What is the reason to disable strict types? The container should be generated to respect strict types. |
Migration from an Anyway, since setting lax types is allowed in Latte, I see no reason to not allow users to do this with the container. A good library is a flexible library. This PR adds a feature that does not break anything and adds no overhead for any running app. |
Latte is something different because you write the code of template, while the DI is completely generated. The goal is for the generator to be able to look at the compatibility of types and avoid errors. So this PR goes against the intentions of the library. You can describe what specific type coercion errors you have encountered? |
Of course. In a NEON file there is a factory or any other service in fact, that has a type hint bool in the PHP implementation. Load an ENV var to that parameter... Bang! class FooService {
function __construct(bool $on){ ... }
} services:
foo: FooService(::getenv(ENABLE_FOO)) This is an oversimplified example, but it demonstrates the problem. I am aware one can solve this with |
Let me elaborate on this issue. In order to enforce correct types in NEON format, one has to use functions I myself am not a big fan of In a function like the following, it is obvious that it returns a string and accepts a string. function foo(string $input): string So while you implement the function, it is guaranteed that you are given strings, whatever the call argumetns are, even without strict types. Then the caller is also guaranteed that the return value will be a string, whatever the inner implementation might be, again, even without the strict types. While implementing services, components or factories I don't care how the caller uses them. All I care about are the type-hinted parameters that guarantee what the input will be like. Now, I understand that the opinions will differ. The thing is though, this is a library and as such it should be made flexible. |
The use of strict types is not based on boolval, strval, (bool) or (string). On the contrary, these type casting operators and functions go against the meaning of strict types. Well written strictly typed code will almost not use them. So even the configuration file must work without casting. Because strict types is about preventing and detecting loss of information. Typecasting operators do the opposite.
Possible solution is to create functions like // no strict types
function intenv($name): int
{
return getenv($name);
} Usage:
But
It is important that Now you can safely use |
Well I am aware of how one should be using strict types. It comes easily once writing fresh code. From my perspective though, where I had to migrate a codebase written for N2.4 that relies on type coercion, the strict types in the DIC were a headache. Of course I turned them off. And I would like to allow to the others to do so without having to hack into the configurator as I did spending an hour to find a way to do it "gently". But I take your point that one should probably invest in making the types safe. What are we going to do with this PR? BTW: your migration guide N2.4 → N3.0 is very optimistic 😊 |
Btw I added some type conversion functions to DI 2fab800 |
Also, "nullable" type conversion would be awesome. And operators like ternary or null-coalescing. Though I don't know how to add those, can't help much there. |
5ddfaf1
to
d41e22c
Compare
38608a6
to
766e818
Compare
47688f3
to
4c651dc
Compare
fa49cdc
to
6c426c4
Compare
680bc12
to
5066242
Compare
b109822
to
7f11e6e
Compare
ef39d2d
to
f729b1e
Compare
a43bb2c
to
71a91be
Compare
51cfed7
to
9c4af52
Compare
0a1f0ab
to
8c392ab
Compare
96c09bc
to
7a40f39
Compare
8655bcb
to
59cf699
Compare
Raw usage:
With nette/bootstrap in a Nette application: