« first day (4939 days earlier)   

3:19 PM
DTOs, rarely, value objects, all the time.
Mainly to create readonly + withers though
 
4:08 PM
@MarkR Part of your responsibilities is doing tech hires right?
 
@MarkR How do you define these two terms? They're often used synonymously. In explain in the RFC why I believe readonly + withers are a bad solution to this problem, especially for growable data structures.
 
5:00 PM
@IluTov DTOs + value objects? Generally the DTOs involve some element of hydration/serialization, value object is probably not the most precise term to use, but if I had an object representing, say, a prepared email message, that was passed between components, immutable, having the withers enforces the immutability, it's less of a factor on DTOs although plenty of those are read-only too, but generally they're unchanged after setup or hydration
if there was clone with { ... } i'd expect the withers to go away, although in some cases they can be handy, i.e. our value object representing an email requires setting to email + name at the same time
 
5:21 PM
I'd rather have Foo {...$foo1, field: $value} syntax than clone with.
The clone with syntax always looks really weird to me.
 
 
3 hours later…
8:02 PM
@LeviMorrison It's just unclear to me how that would work with constructors.
@MarkR Right, but with value types you don't really need withers or clone with at all, because it solves the problem of mutability at a distance without requiring readonly.
 
@IluTov Please link the doc again would you please
 
@MarkR wiki.php.net/rfc/structs-v2 I'm not sure if you've read it, but it's possible I don't communicate this well. I'll think about way to make this clearer.
Essentially, readonly is a way to avoid your data changing unexpectedly from other places. Arrays don't have this problem, because if somebody changes the data you shared with them, they implicitly create a copy from it. Structs work the same way. readonly is fine, except it doesn't work well with clone. Withers are tedious to write, and every mutation creates a clone of the data, even if the previous copy is discarded right after.
Structs solve all of those problems. They are immune to modification at a distance, they don't create useless copies, and they don't require boilerplate code for mutable APIs (like withers would).
Furthermore, they make growable data structures as value types possible, which was the original reason why I even looked at them.
 
I think it's the implicitly bit that's sitting funny with me, I can't think of ever having used something similar that wasn't explicitly copied at some point
 
@MarkR I mean, look at PSR-7. $response = $response->withHeader('X-foo', 'foo')->withBody('...') alone creates two completely useless copies.
 
from spooky action at a distance to spooky behaviour unless you go look at a type that might be <X> calls removed.
I can see the benefits with methods, direct property manipulation with it is melting my brain for some reason
 
8:20 PM
@IluTov Oh sorry, I didn't pick up that the discussion was limited to DTOs. But yes, I agree that immutability works pretty well to solve similar problems as visibility does. :)
 
@MarkR Right, -> might or might not influence a 3rd party, depending on whether the type is a struct or a normal class. I'd say that the same already goes for the [] syntax due to ArrayAccess. E.g. you might expect $b = $a; $b[] = 42; assert($a !== $b); to hold, but it's not true of $a` is an object.
I would also say, does it matter? If we don't have structs, you're going to have to think about this issue regardless. At least structs can solve it for a subset of your types.
 
Well there's the simple case, properties only, in which case $x = $x with { ... } I would imagine could be optimized if the refcount of of $x was 1 as you'd know you were throwing away the first $x? That doesn't seem to work for anything with methods though.
 
@MarkR What if the second write to a property fails? Then you throw, but you already mutated the first property of $x.
 
Hmmm good point
I think a benefit would be that you could set a bunch of stuff at once without needing to clone each time, but it is of course a narrower scope than what you're suggesting
 
@MarkR Right, but I'm just wondering how many fixes it will take to make readonly work properly.
 
8:36 PM
I'd see it as more of a code style convenience, personally. One of the reasons I like named args + promoted property constructors instead of just setting the properties is so I'm not repeating $usefulVariableName-> half a dozen times
 
@IluTov In my head, it doesn't interact with constructors at all. It respects scopes, though, so if there are private properties and you are try to construct one from public, it doesn't work.
 
@LeviMorrison It does to me as well when written with braces. That's why I argued against braces and in favor of array syntax back when it was proposed and that's also what the current RFC uses.
Not sure what's up with introducing braces for key-value pairs in PHP. That's needlessly new syntax.
 
It may interact with constructors in that if it has one, it it does a scope access. Meaning if there is a class with all public properties but a private constructor, then it can't be constructed with brace syntax from public nor protected, just private. The constructor isn't called, though.
 
@LeviMorrison But, if Foo does have a constructor, does that mean that Foo { ...$foo, prop: 'prop' } works, but Foo { prop: 'prop' } does not?
 
8:52 PM
@TimWolla Because [ ] uses quoted keys (usually) and =>, whereas { ... } could use named arg style of { foo: $bar }
 
Named arguments use round parentheses instead of curly braces, though.
 
@MarkR If we wanted consistency, args already use : inside (). Match already uses => inside {}.
Allow both, ultimate flexibility.
 
in constructor X: Y is where X is an arg/property sooo, probably abit more expected, but again, reasonable point
 
Array syntax is the right choice for clone with, though, because it naturally extends to making the right side of the with dynamic and passing an actual array.
 
Now, for consistency, we could prefix it with new. Hmm, this looks familiar :P
 
8:55 PM
i.e. $newProps = ['foo' => 'bar']; $updated = clone $obj with $newProps;
 
If someone told me I had to go quoting my property names for core syntax, I'd vote no on that no matter the other benefits
 
@IluTov IMO it should only be able to update fields and apply fields that are visible, like Rust: doc.rust-lang.org/reference/expressions/….
 
@LeviMorrison What I meant is: If you're not cloning an existing value (so, without ...$foo), how do you create a new value, assign the properties, while ensuring the object is properly initialized, without calling __construct?
 

« first day (4939 days earlier)