« first day (4899 days earlier)      last day (274 days later) » 

09:35
Yesterday I unintentionally did a Brew upgrade and my `icu4c` was updated from 73.2 to 74.2 causing all my local PHP installations to fail with "unable to find dynamic lib", because most probably Brew deleted/unlinked some paths. Since it's pretty annoying to force Brew to get back previous version of ICU, I thought I'll just reinstall asdf's PHP versions. But for 7.4.33 I get:

`/var/folders/gl/056qnnh95yg8yh2wjzsrr7f80000gq/T/php-src-php-7.4.33/ext/libxml/libxml.c:1050:35: error: incompatible function pointer types passing 'void (void *, xmlErrorPtr)' (aka 'void (void *, struct _xmlError
 
2 hours later…
11:13
@Wirone backport the libxml 2.12 fixes from php 8.2
@nielsdos seems like out of my reach 😅. For now I just don't install 7.4 locally, as I don't need it anymore for my main development (GetResponse app was migrated to 8.2), and for the rest I can use Docker.
 
4 hours later…
15:04
that's interesting. So, passing arguments to the object widens requires-types, and passing the object narrows permitted-types. It could be static analyzer friendly but only if the latter could be narrowed only once, I think:
```
function g(container<A|B>) {}
function f(container<A|B|C> $obj) { g($obj); }
f($obj)
$obj->push(new C);
```
Otherwise, $obj is container<req(A|B),perm(A|B)> after calling f, but a static analyzer is not able to infer that, and can't say that the last line is unsafe.
@Crell $a = [1, 'beep']; f($a);: this can be spotted by a static analyzer in this flavor of generic arrays (this is how it's implemented in phpstan and psalm)
@LeviMorrison could you elaborate on how it would help? I'm genuinely curious
15:36
@Trowski If you're generating an array, var_export() is fine as the data is all in shared memory. If you're using an object, you need to hassle with set_state, visibility, reflection, inheritance, and all that BS, and the performance difference from unserialize() is simply not worth it in most cases.
I guess I just don't see the purpose of a typed container where the type is not specified up front. Every use of that I can think of is a bug I would tell a dev to go fix so they know what their data is.
16:35
@ArnaudLeBlanc It's what would feel quite natural for php for me. But I have little idea how practical this is. Would need toying and concrete examples…
@ArnaudLeBlanc And also we could have something like container<in A|B> - i.e. it would just check against the permitted-set without constraining the required-set (basically we want to be able to insert, but don't care about extraction)
basically indicating the variance on the generic (we need that anyway I guess)
17:04
Eh? I don't follow. If you can only put A|B into the container then you know concretely what it will contain: A|B.
@Crell The only purpose of allowing it is to reduce verbosity I think. Rust lets you create a Vec without specifying upfront what will be in it, yet it's safe: play.rust-lang.org/…. In @bwoebi's idea, if the type changes internally but it behaves as if it had the actual type from the start, and if the types could be discovered statically, this seems fine to me
Hm, that enforces the first type that's used. It doesn't let you add a string and then an int. So it wouldn't grow the type list dynamically. I suspect it's doing some compile-time inferencing to declare the Vec type.
 
1 hour later…
18:23
@Crell yeah, the hard part is ensuring that something isn't simply Vec<firsttype> or Vec<mixed> by default, but actually dynamic enough.
That's what I don't see a use for; and I'm not sure I'd want even if we can do it. :-)
Vec<firsttype> is probably doable, and fine.
@Crell It's not, and the most trivial reason is just interfaces. You don't want Vec<concreteImplOfInterface> but rather Vec<interface>. It's just a big footgun then.
It works as long as the firsttype happens to impl the same interface, then you integrate it with other code which suddenly has another interface impl and it blows up.
In that case, just $v = new Vec<Interface>.
Yes, then you will have to fix your code afterwards. Instead of the language just making it natural for you.
It's very … un-PHP.
I'm confused how it's actually an issue here...
18:29
With rust you have a very strong type inference and it will consider all accesses to the object within your scope. That's okay. However static analysis in PHP will never be able to figure out all types ahead of time for all code.
`new Vec<Interface>` - create an empty Vec to hold Interfaces.
`new Vec($foo)` - create a Vec to hold whatever type $foo is, and put $foo in it.
`new Vec<Interface>($foo)` - create a Vec to hold Interfaces, and put $foo in it.
But then you do $vec = new Vec; $vec->add($foo);
I.e. the constructor call has 0 info about the type
That should either be the same as the second example, if we can figure that out, or an error if not.
I don't want to play guessing game at how strong PHPs type analysis is when writing code. That's bananas.
I mean it's an error unless we can reliably make the engine figure it out. If not, error. No questions.
If you want "traditional PHPish behavior", use mixed. :-) If you're bothering to specify generic types, I am completely OK requiring you to actually specify your types.
18:32
I cannot pass my Vec<mixed> into your library expecting Vec<Foo>.
Correct. That's a feature.
Yes. Hence just using mixed is not an option.
You can't pass your array to my library expecting ProductList, either. I don't see what the issue is here.
One of the strengths of PHP is writing code quickly. Thinking about generics is not going to help.
There's a big reason why inference is so popular in statically typed languages.
I feel like we're talking past each other somewhere...
18:35
Now we're bringing generics to a weakly typed gradually typed dynamic language …
Might be. Not sure where though.
function a(ProductList $p) { ... }
$l = new ProductList($product);
a($l); // OK
a([]); // Type error
yes, that's obvious, not even arguing here
I'm just arguing about the flexibility of determining the generic
function a(List<Product> $p) {}
$l = new List($product);
a($l); // OK
$l2 = new List<User>;
a($l2); // Type error
yes, and now:
$l3 = new List;
a($l3); // ok - will be empty List<Product> now.
// What you seem to be suggesting:
$l = new List;
$l->add($product);
$l->add($user);

function b(List<Product|User> $l) {}

b($l); // OK

// Which I see no practical purpose to.
18:40
yes exactly that should work
Just like:
interface Product {}
class Product1 implements Product {}
$l = new List;
$l->add(new Product1); // starts out being List<Product1>

function b(List<Product> $l) {}

b($l); // OK - now confined to List<Product>
I don't see how that can be checked or enforced ahead of time. Or why $l = new List<Product> is problematic.
Static analysis will be able to check that ahead of time within some limitations. It doesn't need to be able to able to check this in 100% of cases. Just like static analysis nowadays cannot for other code.
And I'm explicitly not wanting for enforcing ahead of time.
/me is on team "all errors are compile errors", where possible. :-)
The type should be enforced dynamically.
(Yes, yes, I should just go learn Rust.)
18:46
@Crell Yes, honestly.
It's just not what PHP is (nor should become).
@bwoebi No, that is just terrible. That would be difficult to use and I'd imagine difficult to implement.
@Trowski Why would it be difficult to use?
How do I know I can pass $l to a function expecting List<Product>?
By knowing what you just created?
And if it's from somewhere else, I hope you have an appropriate type put on your function or such.
Until someone adds a non-Product to the list and all seems to work well, but blows up at runtime.
It should be throwing when adding the item if it's of the wrong type.
18:54
@Trowski yes. Just like as if I would assign something different to $l and it'll also blow up at runtime when $l is being used.
Yes I suppose both are runtime errors, but I'd rather have the error occur when adding to the list rather than later when trying to use it. It's much easier to spot the error knowing ahead of time that the list should only contain products.
@Trowski If you want to do that, you're absolutely free to write new List<Product>()
TypeScript for example would require you to write new List<Product>. Types should be explicit, not implicit.
But you're not required to.
And depending on the complexity of the type that may be a good thing.
Typescript's typing capabilities along with PHP having an officially recognised static analysis ruleset and slowly moving to add them at runtime would be incredible
If I had to guess, any such path would do something like assert where more costly checks were initially only performed in dev mode until a proper optimization could be done
19:29
I can't imagine implementation would be easy. I'd think the template types would need to be determined upon creation to be performant.
@Crell $l = new List<Product> is verbose especially when you have multiple type arguments and you need to type this repeatedly. This also applies to functions: array_map has 3 types args (key type, original value type, new value type) and specifying them is super verbose: array_map<int,Foo,Bar>($cb, $ary).
To be clear I'm totally fine with detecting the types from those passed to calls, including constructors. It's pretty rare in TS that I need to specify a type on a function call, the same should be true for any PHP implementation.
Inferring from a typed property would also reduce the need to type out those generics multiple times.
using the runtime type for function calls can be an issue if the function uses the type at runtime like f<T>(t: T) { return new List<T>() }, as T may be too specific
19:45
Right, then you need to declare it. Same is true in TS.
That's essentially a constructor though, which is where I'd expect most declarations to be necessary.
@ArnaudLeBlanc If the type(s) can be derived from the same statement, then inferring them is totally reasonable. It's the "magically expanding type until you use it" that feels like quantum uncertainty to me.
$l = new List<Product>();
$l = new ProductList();

// The difference from what we have to do now is minimal.  It's literally just the two <> characters.
If we can reasonably infer it, we should. I don't think anyone is disagreeing there. It's the "if you cannot infer it, let it be whatever until we try to use it, then see if it works" approach that strikes me as problematic.
20:02
Admittedly that approach does seem very PHP.
The PHP we've been moving away from since 7.
20:13
@Trowski TS is different in that it is fully erased and there is no runtime types. f(v) infers T from the static type of v, which almost always results in what the programmer wants. What I meant is that inferring T from the runtime type of v would more often not.
Good point, that's going to usually result in a narrower type than what was wanted. Hmm…
Inferring might have to be limited to typed assignments. So then we need typed local vars too. :P
At which point you're typing out the verbose type anyway.
20:30
I see at least two challenges with this approach:
1. Enabling previously disabled type checks can be a breaking change because of coercion (although the inverse would be a bigger issue)
2. The type system would need to be designed with the VM/compiler in mind, otherwise it may never be possible to integrate it (because of performances). And even then, flaws could be found during the integration and it may be too late to fix them (because of BC)
I think I see where @bwoebi was coming from. It's pretty rare an object isn't soon assigned to a property, passed to or returned from a function, so having "unknown" be the template type if not provided might be very practical if we can't infer them in a useful way.
@MarkR BTW, runtime checks aside, what would be the benefits of an official static analyzer when compared to phpstan/psalm?
21:02
@Trowski I think you've got it :-)
$l = new List();
$l->add(new Product());
// $l is now List<Product>. I'm OK with this.

$l->add(new User());
// $l should not magically become List<Product|User> here. Bob says it should. This seems to be the main point of disagreement.
@Trowski I mean even pre-PHP 7 it was common to have class types on functions. It's been largely the same PHP with gradual typing, just that you can now actually specfiy more types. We're never requiring types more than we've been in the old past.
@ArnaudLeBlanc Mainly that if the PHP engine ever enforced them at runtime, there'd be a pre-emptive "we officially told you so". The same can't quite be said for psalm/phpstan, although I'd expect PHP to copy them almost exactly, as it's a mature system
It doesn't need to be a working static analyser, just a spec, and support in the language for directly embedding the extras into the code, even if erased e.g. var $foo = new Array<Type>
@Trowski I absolutely want that you're going to be able to write array<Foo<Bar, callable(Bar): void>>. But I don't want to require you to write this. Especially not in every single place you just touch an object of that type.
@Crell This isn't great though if you wanted a list holding objects of an interface, which is likely to be pretty common.
21:11
Then you specify new List<ProductInterface>.
Are there any languages with Generics that do what's being described here?
@Crell They don't have to because they can statically infer it to 100%.
@Crell you say that until you'll have to specify List<callable(ProductInterface, User<AdminPrivileges>): DelayedEvaluator<bool>>. For a simple generic of just one trivial generic arg nobody bats an eye. For any sufficiently complex type it gets in your way. And that's why languages do have auto (C++), var (c#), _ [as in Vec<_>] (rust).
One of the nice things TS has going for it is the ability to define multiple things inside the file, without risk of collision. In that case it would make sense to define a type for the callable
One of my personal wishlist items is the ability to scope a class to a file, especially for things that are heavy on DTOs, being able to define them all in one file and not have to deal with collisions would be fantastic
Rust doesn't accept mismatched Vector types: play.rust-lang.org/…
and if I had to guess, a lot of the groundwork is already there with use statements
@MarkR In PHP inner classes could probably solve that…
@TimWolla rust doesn't support unions
21:21
@bwoebi I was thinking something like "local class Foo { ... }" and the tokenizer would mangle it to Foo_WithTheFilePathEncodedSomehow and then do like use statements do, and alias Foo to Foo_WithTheFilePathEncodedSomehow
@MarkR sounds magic
It's very similar to how private properties are mangled isn't it?
Are there languages with both generics and union types? (I honestly don't know.)
@Crell well… typescript
@bwoebi Then I don't follow how Rust is relevant in this context. It doesn't accept two different types sharing a common trait either, without explicitly specifiying the trait type.
21:26
Does TypeScript do what you're describing? It's naturally an odd duck as it's all type erased, so by runtime none of it matters anyway.
@Crell exactly, hence it's not really useful to compare it here. And yes, it actually does.
It feels like the general point is "there are cases where generics get very verbose." And... yes, that's true? :shrug emoji: We only need to make the common cases easy and the rare cases possible. We don't need to make them all easy.
@TimWolla I was just talking about automatic inference. Btw. on the rust side it can't even coalesce two match arms when the impl the same trait, you'll have to cast it.
I don't think anyone is arguing against inference. My understanding is that what's being argued against is automatic widening during inference.
Bingo.
21:32
Yes, and it sounds like having two objects of different classes implementing a common interface in a List would be a rare case that we should be verbose in that specific case.
Not because it's a rare case, but because there are multiple possible types if there are multiple interfaces shared by the list entries. Stringable comes to mind as an interface that would be seen somewhat often - and of course everything is a mixed. Would you expect it to infer the most specific type matching all the values?
class Foo implements A, B, C { ... }

$fl = new List();
$fl->add(new Foo());
// What even makes sense to infer here? List<Foo> is the only thing that makes sense to me.
FWIW, TypeScript will just infer any (unless I'm holding it wrong): typescriptlang.org/…
@TimWolla Not quite. I would expect all the added types to be validated against constraints the value later encounters. I.e. you add "Foo" to the containted typeset. Now when something is passed to List<SomeInterface>, the typeset will be checked against SomeInterface.
And it will actually become a List<SomeInterface> (If it's not a covariant generic at least)
@TimWolla you have array on line 7 instead of p
Whoops, indeed. Nonetheless it infers array to be any[] when hovering it. I forgot how to get the compiler to output the inferred type (I believe it's possibly via a comment or so).
21:41
@TimWolla If you hover on the call to line 10, you see it's (string|number)[] - before that call it's any[].
It's gradually restricting the type (and internally keeping track).
@TimWolla The array becomes (number|string)[] typescriptlang.org/…
It's been a while since I did TypeScript. The following is probably what I had in mind: typescriptlang.org/….
@bwoebi beat me to it. :-D
That's a good example of inference resulting in a narrower type than you may have wanted.
@TimWolla Yeah, that's an idiom in typescript that [<something of type T>] results in T[].

« first day (4899 days earlier)      last day (274 days later) »