« first day (4910 days earlier)      last day (32 days later) » 

3:15 PM
The more I think about it, the more I am opposed to ADTs/enums-with-data being mutable at all. They're a compound value type. They have no business changing out from under me.

Collections are a different creature. Technically both Option and a Collection can be implemented as a monad, but that doesn't mean they need to share all behavior. (Monad is such a broad pattern that variation between implementations is to be expected.) So "collections are mutable" doesn't really carry any weight for me when talking about ADTs.
 
> The more I think about it, the more I am opposed to ADTs/enums-with-data being mutable at all. They're a compound value type. They have no business changing out from under me.
Why are you so against mutability? I get you don't want things to change unexpectedly but... pass-by-value, copy-on-write solves that...
 
1. I cannot think of a use case where wanting to change the value inside an ADT isn't a code smell indicating I'm doing something wrong.
2. AFAIK, Option, Result, and other enum-based ADTs are immutable in every other major language. The odds that PHP will be unique *and* correct by going the other way are highly remote. This is a case where "this is how everyone does it" really is a very strong argument.
3. Related to 2, I don't know all the possible places that mutable enums could break in weird and unexpected ways. I do know that immutable values have fewer places they can break, thoug
 
@Crell Uh... rust allows them to be mutable?
 
It does?
 
Of course?
 
3:23 PM
link?
 
@LeviMorrison bad link
 
Da'fuq, Rust? :-P
 
Immutability is okay, but you really seem to want to hammer it onto things that don't need it. PHP's pass-by-value, copy-on-write arrays are really nice, and the only thing bad about them is when you do need to mutate the same one, you have to use PHP references, which suck compared to out parameters that most languages have.
 
@Crell ADTs are essentially just sugar over a fixed amount of data classes implementing a common interface
(with data class I mean what Ilija has been recently working on)
 
3:31 PM
I think having pass-by-value, copy-on-write types available to the user would be really nice. Much nicer than immutable types and having to use ->with garbage and insane memory allocator pressure everywhere.
To be clear, I'm not against immutable types. I think they do have a place. The place is just very small if you have pass-by-value, copy-on-write types also.
 
Is it really that much pressure? I recall when MWOP was benchmarking PSR-7, he found it only marginally costlier to use immutable objects.
 
@Crell It depends on which scale you use it. For a classical config object or PSR-7 which doesn't get reconstructed all the time it's probably fine.
 
@Crell I work on a team for a company which writes a product to observe allocation overhead, so take that context in mind but... yes, IMO.
 
If you structure your codebase around using ADTs everywhere, then it will very much make a difference.
 
Exactly, if all ADTS must be immutable, that would be bad for memory allocation pressure.
 
3:34 PM
Give me a struct-like object with proper initialization syntax without using a constructor, a partial, and a splat clone, and i'd delete 80% of functions beginning with 'with' as soon as it landed... (aka typescript interfaces)
 
Ones with single references (like many properties on objects would be) would not often not require allocations in a copy-on-write type (though sometimes they would).
 
@MarkR CPP / named args already get you 90% of the way there. We just need clone with or equivalent. :-)
 
@Crell Yes, until you need to extend them, then just kill me
 
@MarkR Valid, that is more annoying that it needs to be. At least for the implementer. It's no different for the caller.
 
and at the point we have 'clone with { propName: value }' might as well make it an initializer too, with some splatting, and hurrar
 
3:37 PM
@LeviMorrison That seems to be specific to Option tho? Result doesn't seem to be able to do this looking at the Rust docs
 
@Girgias you can take a mut ref to Result and replace it with whatever…
There's just no builtin function for that
 
@bwoebi I've never done Rust
 
@Crell it is for the caller too - have you ever taken a config object with a mega constructor ... and then you want to create a new, slightly different config object where you forward every single arg … xxx: $original->getXXX() a twenty times
 
@Girgias Example of what Bob means (I think): play.rust-lang.org/…
 
This is cursed
 
3:41 PM
@Girgias No it isn't
 
Difference of opinion
 
Difference of experience with rust
 
4:00 PM
@bwoebi That's why I have written thousands of words on why we need clone with or similar. :-)
 
@Crell yeah, but what we really need is just data classes
There's no reason why a config object should be by-ref
 
@bwoebi I've abused reflection more than once to do that.
 
Data classes in themselves don't change the construction signature.
 
@Crell If you take property hooks with data classes together, there's no need for CPP anymore for config objects
you would just use simple properties, with validation checks as necessary
I.e. data classes will allow you to get rid of 20-arg constructors…
 
... Methinks we have a different definition of data classes.
I am looking at just adding a flag so that an object passes by value. No other changes.
(Possibly some default implementations of __toString or such, like Kotlin does, but that's TBD.)
 
4:09 PM
Oh, a data class is just adding a flag. I'm just saying, the reasons for having 20-arg constructors vanish with data classes and property hooks.
 
How?
 
Let me ask the other way round: why would you ever have 20 args in a constructor currently?
And I'll tell you how it won't be needed anymore.
 
Because it's less work to type than 20 $foo->var = $val lines.
 
@Crell I'll gladly prefix 20 lines with $foo->, which forces me to write names, which is arbitrarily re-orderable and can be moved around, with ifs around it, etc.
 
Named args solve all but the last one.
 
4:19 PM
named args allow to write names, they don't force.
and moving around means also computing the value over multiple statements
 
4:50 PM
Not necessarily.

Honestly, I've yet to have an issue with struct classes being "too big" for me in practice.
 
 
3 hours later…
7:28 PM
I'm in the camp of less repetitive code is usually better. Needing to access the same property 50 times is unnecessarily repetitive, and I also like constructors because the IDE and Psalm will yell at me if I fail to provide a value to one
 
7:51 PM
@LeviMorrison CoW for enums seems very niche. How often do you really have an existing enum instance that is uniquely referenced that you also need to modify? It might be convenient, because it avoids typing out all its associated field to create a new instance, but I'm skeptical it's going to make a meaningful difference irl.
But again, I'm not strictly against it. I just don't know how it would work. $this = Option::Some($value) would not actually solve that issue, because creation of the new enum is still necessary for the assignment itself. Of course, this could get compiled away somehow, but not sure more complexity is a good solution.
 
@IluTov Yeah, all the time. It's a member in some object and we don't really give references out to the member.
 
@LeviMorrison I see. Still not sure how we can make this work, without introducing more special syntax or complex optimizations.
 
8:30 PM
@IluTov I haven't thought more about $this = Option::Some($value). I think $option->replace!(42) which returns void (no object copies caused by returning previous Option) could be valuable, and I don't think that would necessarily require anything special?
 
@LeviMorrison replace is a one-off solution for Option. Wouldn't you want this for other enums? What if you have Result?
replace implicitly includes the Option::Some discriminator.
Other enums will obviously have other discriminators, but also different values, and different amounts of values.
 
Right, but they can write a methods for the enum in such cases, and the tech is there for copy-on-write for the method, right?
 
@LeviMorrison But how would you change the discriminator within a mehtod?
 
I was trying to point this out, that you can't use objects as the implementation of discriminators, or at least if you do, they have to be sub-objects (not efficient).
 
All this talk of enums, I'm still wanting to be able to use them as array keys..
 
8:35 PM
And this makes sense. Things like the fact you can't do fn (Option::Some $val) in the language (or most languages which allow this) also point to implementing it differently.
You don't work in memory with discriminants as a unit of work. You work with the whole thing.
 
@LeviMorrison Forgetting about the implementation details here, changing the discriminator means you'll need to provide all of the new discriminators associated values to end up with a valid object state. I can't see a way other than writing to $this to do that.
 
Can you say that a bit differently? I didn't quite follow.
 
@LeviMorrison Ok, can you show me how you would implement Option::replace in userland?
 
Not sure if it can be done efficiently without additional language support. Let me think a bit.
 
@LeviMorrison Right, that's what I'm thinking. If $this was an Option::None, you need an atomic operation that sets both the discriminator and the value. The same applies for other enums, of course.
 
8:42 PM
Yeah, I can't think of a way to do it without additional language support.
 
Rust uses mem::replace, as previously mentioned. But for them, constructing the value to be copied is cheap, because it lives on the stack.
We could do something like $this->replace($caseName, ...$args), but that would be a generic interface.
 
Right. Logically each case is just the discriminant name with its args. The implementation would have to verify the correct args for the given discriminant, of course.
 
Yes, which is somewhat redundant, because Enum::case() already does that. But that's not too bad.
 
Right. The nuance comes from efficiency.
 
That said, replace cannot return the previous value, because the object might be modified in place. Constructing a new array to be returned defeats the purpose of avoiding allocations.
 
8:53 PM
Correct.
 

« first day (4910 days earlier)      last day (32 days later) »