« first day (4939 days earlier)      last day (224 days later) » 

08:40
@MarkR you might like gist.github.com/azjezz/a95f641383801e9eb8f88003df0b297d proposal ( this just refers to the syntax )
@IluTov i wrote something down, kinda messy right now, but it is something: gist.github.com/azjezz/7da4970c68f7dd48f3ad392ccfa27588
09:29
@azjezz Seems about right. I believe the with / clone with { ... } syntax has been shown in a cuople of other places too
09:50
@IluTov "Objects are passed by reference, rather than by value". This is false / confusing, because references are something different in PHP. I'd say "The object handle is passed by value". I believe this distinction would also make it easier to explain the exact semantics further on.
Because when only the "object handle" is copied due to the by-value pass, it's more clear that the underlying object is not copied.
 
2 hours later…
12:20
@IluTov When people recommend composition over inheritance, they should perhaps also mention that the visibility system in PHP is tailored for inheritance (private, protected) and not composition (no package-visibility yet).
Assuming encapsulation is important for people, sometimes I'm not even sure...
 
1 hour later…
13:32
Morning
\o/
@PeeHaa Getting ready to split my role up at work... hopefully about to have more time for life again (aka video games and other fun stuff!)
\o/ Well that's about time :)
14:14
@StatikStasis \o/
14:30
@TimWolla Sure, reference is an ambiguous term. I'll make sure to use something else.
@Tiffany happy to be back here too :D i hope you find something soon too!
@OlleHärstedt Do DTOs often contain visibility-protected fields though?
15:19
DTOs, rarely, value objects, all the time.
Mainly to create readonly + withers though
16:08
@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.
17:00
@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
17:21
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…
20:02
@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
20:20
@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.
20:36
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?
20:52
@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
20:55
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?
It's limited by visibility. You trust those with write visibility, same as any other write to fields.
@LeviMorrison That's wasn't really the point. You could have private properties being initialized in the constructor. Not calling the constructor breaks assumptions within the object that it is properly initialized. In comparison, Rust forces you to specify all the fields of a struct. But even then, your constuctor may sanitize the params in some way, and then assume that they are properly sanitized.
21:02
You are right, I don't understand your issue.
If there is a private field, what stops you from invalidating the private field by some other write within the class scope?
@LeviMorrison Within the class scope, nothing. But we wouldn't be limiting this new syntax to the class itself, right?
@LeviMorrison Asked differently, what would happen in this code? 3v4l.org/9lfMr
Or are you just suggesting not to support constructors at all?
If you have a constructor I don't see how { ... } would be allowed
@MarkR IMO, that's just confusing. I'd rather auto-implement a constructor for structs then.
@IluTov Assuming I didn't overlook anything, it's fine. It's the same as doing basically this: 3v4l.org/qaRvd.
But then again, we already have constructor property promotion. So I'm not sure that's really necessary either.
21:09
@LeviMorrison And what if you have parameters with types that are objects with a constructor?
@LeviMorrison Passing empty strings as defaults that the user never specified does not sound like a good idea at all.
@IluTov No, you misunderstand. That's not the implementation. I'm saying that your "access control" didn't actually restrict anything, and this wouldn't be any different?
@LeviMorrison Ok, I don't follow then. :)
Isn't that what we do with default args on property promotion anyway? If you have a constructor, you must use the constructor, if you don't have a constructor, allow creating using new property struct syntax, and throw an error if it doesn't provide every property that doesn't have a default
@IluTov The constructor uppercases things, right? However, the property is public and writeable. What stops you from changing it to a non-upper case string? Nothing.
21:13
@LeviMorrison Right. I understand that part. But even if you change them after, how do you call the constructor if you don't know what values to pass to them? You could try to figure out from the initializer list, but nothing guarantees that they'll match the constructor args.
@IluTov You don't.
The constructor is not called.
It's like newInstanceWithoutConstructor.
We respect its visibility, though, unlike that thing.
Honestly, as complex as PHP is getting, I don't know if this will work. Devil in the details and whatnot, and things like readonly and such.
21:50
Not calling the constructor breaks interior assumptions, e.g. constructor initialized private fields, or setup of other sorts. Some serialization libraries use newInstanceWithoutConstructor, but they need to be careful to leave the object in a valid state when they're done. Not calling constructors would also mean that adding any such constructor code that is expected to run would become a breaking change.
Users would also need to look at the implementation of __construct to understand whether using the syntax is safe.
@IluTov Private fields are only accessible by the class, and a class needs to be able to trust itself...
@LeviMorrison The constructor initializing the field is trusting itself, no?
Users shouldn't have to look at a constructor. If something is inaccessible, they are locked out of the syntax.
But if some 3rd party uses Foo {} without initializing the private field, the object itself doesn't expect that the constructor wasn't called. All code accessing the private field will now need to deal with !isset.
They can't. A third party cannot initialize a private field. It will fail, because it's not visible.
I just noticed that the RFC on casing didn't mention stdClass at all. Somehow I feel like this is an important oversight lol
21:56
@LeviMorrison Ok. So if the struct declares a private field, or maybe more appropriately, when it declares a constructor at all, this syntax breaks. That just significantly restricts its use, and becomes a BC break when a struct adds a constructor.
@LeviMorrison How so?
@IluTov I don't know, I just feel that way.
@IluTov A constructor can be present. Its visibility is respected. Right now I'm struggling to find cases where you have public properties with a public constructor that are broken, aside from things like readonly as mentioned previously. The public property generally means anything type-safe can be written to it at any point so... effectively ignoring the constructor (although again, respecting its visibility) and just writing those properties should be allowed, yes?
@LeviMorrison Maybe I didn't think it through, but stdClass just remains a reference type. An alternative with value semantics could be useful, although at that point you could probably just use an array.
@LeviMorrison Right, fully public properties are not really my concern. They may change arbitrarily.
@IluTov I meant that in two RFCs that talk about class naming and specifically their casing... how did neither one mention stdClass which doesn't seem to follow any convention at all?
@LeviMorrison Oh, I'm sorry. I misunderstood, and thought we were talking about structs.
@IluTov No worries. I switched topics suddenly lol
@IluTov So what things are you concerned about? Doesn't need to be struct object specific, class objects should support this too. I still feel like I don't grasp your arguments.
22:08
@LeviMorrison If the syntax is allowed in the presence of __construct, if breaks interior assumptions. If it doesn't, it introduces BC breaks when adding constructors, and is just less useful overall.
@IluTov Give me an example of an interior assumption that you think would break, based on what you understand from my not-written-down proposal.
@LeviMorrison Private properties will remain uninitialized. But you already said you'd disable the syntax in that case, so only really the latter half of my last message is relevant.
To be clear, the class itself could use this syntax to initialize private properties, because they have access to them. It's not just "ah, there's a non-public property, can't use this feature on this object!" It's visibility based to the scope calling it.
I understand, but the syntax would still break everywhere else.
I'll think more about the supposed breakage when adding a constructor but... so far I haven't found any issues when thinking through it.
22:13
My brain is not really working at full capacity, maybe I'm missing some things. :) Some sleep might help. 😴
@IluTov To be sure, you mean that there is a class with only public properties today, and tomorrow, you add a private property. This is now a BC break because callers which used brace initialization are now broken. Something like this?
@LeviMorrison Right. Which might be less common for structs, but probably quite common for classes.
@IluTov I believe this is a valid complaint, and one written about in the Rust community. Adding the first private field is a BC break. You'll see sometimes where people add a private field that does nothing just to avoid this kind of thing.
@LeviMorrison I see. I was wondering how Rust solves this. In practice, I would expect most libraries to create new initializers for their structs? I have no idea though, I have never done anything productive with Rust.
Someone even wrote an RFC about this space: github.com/nikomatsakis/rfcs/blob/extensible-enums-and-structs/….
Basically, this would disable downstream consumers from pattern matching and such:
pub struct S {
    pub f: F,
    ..
}
The exact syntax is because it's like the inverse of let S { f, .. } = s; in pattern matching.
22:23
"10 years ago" :D I'm guessing #[non_exhaustive] should do something similar today.
The most recent discussion seems focused on some kind of non_exhaustive attribute.
Jinx

« first day (4939 days earlier)      last day (224 days later) »