@Derick Thanks; you were right; I hadn't installed some packages, and of course, I couldn't because I didn't have some other libraries :D Finally almost solved it.
only in the same way that $undef .= 'foo' is the equivalent of $a['undef'] .= 'foo' surely?
maybe other people conceptualise it differently, but to me []= and .= feel like basically the same operator on different data types
I know []= isn't technically one operator, because you can do special things like $ref =& $foo[]; but in most uses, it's an operator form of array_push
The difference is autovivification, if you skip over that bit then you're right it's practically the same and should be treated the same.
and personally, I'd be fine keeping autoviv to arrays rather than variables, but that's an argument that needs to be made as it'll be a fairly big change
I don't really see the difference; maybe there is in Perl, where that fancy word gets used a lot, but in PHP, any unset variable can be used as null, and null can be used as any type
you can have all the same bugs failing to initialise arrays and then pushing to them as you can have failing to initialise strings and then concatenating to them
@MarkR isn't that exactly the change you have made, with the RFC that just passed: variables will no longer be auto-vivified to string by the .= operator, because it will be an error
I think the underlying difference is that autovivication works on the write behaviour of the underlying storage, whereas null to string coercion depends on the behaviour of null.
implementation-wise, you could have an optimised implementation of .= using smart strings, but it's behaviour is consistent with taking the current value, defaulted to null, and coercing to string
the behaviour of $foo[] = 'bar'; feels consistent with that to me, too
yeah, maybe someone will find a way of explaining the reasoning that gets through to me, because right now I'm not getting it; but thanks for trying :)
@SaifEddinGmati yeah, putting them in a kennel. Will need to break every six hours ish to let them walk and use bathroom. Need to have water readily available. Also, using vet-prescribed antianxiety for them.
@Ekin I've hired people to pack and load the moving truck 😂
type aliases is a tricky one though, as ... where do you define them, and how would they interact with autoloading? They're almost like "classes" on their own perhaps...
Yeah, those have the largest design challenge still. Most of the rest of the things I want to see have self-evident design, it's just a matter of committing the resources to do it and agreeing on it.
Is this idle curiosity or are you asking for a PHPF friend? :-)
For me personally, functional stuff and data modeling improvements (which would include operator overloading and accessor metehods, for instance) are the big wins. I think those have the most bang for the buck.
No doubt. Hence why I think figuring out the bare minimum to expose to user-space to make the rest of it possible in user space would be ideal. Which probably means seemingly unrelated features that dovetail together to become a stream API. Like, function concatenation doesn't sound like a stream feature, but if stream processing happens through functions and generators, then being able to concat generators together is a stream API. And that could be a much easier lift.
Assume |> is the pipe as discussed in the past in some form, and ||> is the same thing for generators. (waves hand a bit). Now you take a file name string, open it and get back a stream object, which gets passed to gunzip() to decompress it, which gets passed to readline() which reads off a single line at a time generator-style, each line gets parsed as a csv, then that array is turned into a data object, then the object is saved.
That's a stream processing API, right there. With hints of functional-reactive style. Also, incredibly composable and flexible and extensible from user-space in ways the current API isn't even slightly.
What are the pieces of that? We need functions that take a stream and return a stream, so we need a stream object. We need function concat/pipes. We need some kind of generator pipe (which I suppose is yet another syntax for map?)
I'm likely missing a few bits here and glossing over some stuff. But the point is that if we break it down, there's only one really stream-ish bit there: an object that represents a bytestream. Everything else is generally-useful functionality (pipes, composition, generators, etc.) Those are a lot easier to conceptualize and build. They combine to give us a stream API, but also give us a lot of additional benefits along the way because all but one of those components is useful in any code.
Like how Sara's post to the list was effectively "if we had a monad operator string streams become easier." Which is true, but also anything else that could use a bind operator becomes easier. That it's useful when operating on a Bytestream object in particular is almost incidental.
so readline is called multiple times, but parse_csv is called once? and why is loadDataObject also called once? this doesn't make any sense to me honestly.
oh |> vs ||>, okay, a bit more sense, but still confusing.
As I said, totally spitballing the syntax and making ||> up on the spot. :-)
But the general idea being that if you have a common API for chaining generators together (whatever it is), and you can represent a stream as a generator-capable object, then boom, you have a chainable, composable stream API.
Doing something one-off for streams specifically would be a bad way to go about it. Figuring out what streams would need and generalizing that to a common language feature makes it both more powerful and easier to implement.
Possibly. I haven't thought through how |> or function concat would interact with generators. But the point is, if we solve that problem, we get a stream API for free.
$filename |> openfile('r') |> gunzip() |> readline(PHP_EOL) |> amap(parse_csv(...)) |> amap(loadDataObject(...)) ||> amap(save(...)); - I guess it would look like that. Which isn't bad, but we'd probably want some better concat then so that we can merge the callbacks and map them once, which is better for performance.
I mean I am not in the mood to get off on the tangent of |> being like hack or like every other language in the world that uses |> right now. I'm talking about a stream API that exists implicitly as a result of better function composition and generator tools.
@Mwthreex if you're using GD, you either have to find that area manually (basically loop over all pixels from the outside and stop as soon as there is a non-black pixel); you also could use imageautocrop with IMG_CROP_BLACK, and then readd the borders. Imagick may have sth. more suitable built in.
@IluTov Haskell, F#, OCaml, Ruby, and Elixir that I know of. It's been proposed for Javascript, but apparently keeps getting blocked by people wanting PFA first, and PFA not passing. (Deja vu.)
Some languages also have a separate function concat operator.
PowerShell might be a more relevant example than bash; I don't know the details, but its pipes are object-based, rather than text-processing like classic *nix shells
@Crell If we had basic scalar methods for the most common array/string things, would you still want pipes? I think they cover like 90% of cases, which I think are also the most intuitive ones. I don't find $filename |> openfile($$, 'r') |> gunzip($$) |> readline($$, PHP_EOL) |> ... very readable.
Hello guys! I've installed imagick in my docker container and php 8.1 Everything looks fine with PHP intervention, but when I want to load an image in this path:
Maybe it's the spacing, but especially without $$ my brain looks at $string |> openfile('r') and completely dissociates what's on the left with the function call. I'm sure that's mostly unfamiliarity but -> is already very ingrained in peoples heads.
Maybe it's also just that I don't think of file paths opening a file, but opening files requiring a file path.
@Derick This file is in a volume; if the container didn't have the permission, I must not have been able to read it by some other commands like file_get_contents
@IluTov IMO, the only problem with this would be to select to "methods". Everybody likely has their favorites, and many will spend lots of time bikeshedding.
Yes, I would still want pipes because they have uses beyond the basic string/array stuff. That's admittedly a lot of them, but by no means all.
Also, it is more performant to do amap($func1 . $func2) than it is to do amap($func1) . amap($func2), despite them being equivalent. Easy function concat and pipes makes that refactor trivial. Methods on arrays would not.
Having been working with them, frankly I suspect at least 80% of the distaste for pipes is simple lack of familiarity and experience. Which I get; that was my response when Sara first proposed them years ago. But at this point, "take a thing, do a thing to it, do a thing to that, do a thing to that" is just the natural way I think, and that's what pipes/concat are.
And it doesn't create confusion about whether something is an object or a scalar, which then "sometimes matters, sometimes not"
@cmb I can imagine. My idea is to start with literally just 1-2 functions, lay the groundwork, and allow future RFCs that focus on individual or groups of related methods.
Might still not work due to bike-shedding, but we'll see.
array_map() equivalent for pipes. Returns a callable that takes the array to map as its only argument.
Slightly irrelevant in this case; my point being that $arr->map($f1)->map($f2)->map($f3) is inefficient. It's better to concat the functions together first, and then map them all at once.
@MarkR I don't know if there are 5 obvious ones ^^
@Crell Although you could still do $arr->map($f1 . $f2 . $f3) without pipes :P
@MarkR When it comes to strings, there's the whole question about unicode (which other people know much more about). For arrays, the question is mostly if we consider lists or dictionaries the default, or if we make every method name very explicit.
@IluTov Well if we're talking scalars in general, the string ones, substr, pos, tolower, toupper, compare, replace, repeat, trim, ltrim, rtrim, length (property?), split
That still requires a function concat operator. :-) And if we've done that, then doing pipes as well seems obvious. (The incremental work of one over the other is, I suspect, very small, so may as well do both.)
@IluTov Because internal functions don't allow trailing args, we have to make every method explicit. I've been bitten by this many times working on Crell/fp.
Sort can be solved by returning a copy (maybe), the $this bit I remember discussing with Nikita, and fixing it would be a huge faff that would need to have a second opcode route to create a write ref context depending on the resolved function, to avoid having to do every operation in write-like context
@IluTov Good question. For consistency with array_map i'd say probably maintaining keys, to pack it'd be ['a' => 1, 'b' => 2]->map(fn($x) => $x * 2)->values()
@MarkR But then do we do the same for filter? That's often a major wtf (e.g. return $this->json($array->filter(...))` suddenly returns an object instead of an array.
a JSON object I mean
@Crell That's what I meant with "For arrays, the question is mostly if we consider lists or dictionaries the default, or if we make every method name very explicit."
I agree with Mark that we should by default retain the keys, BUT, if you want to use the key in the callback (sometimes you do), that needs to be a separate function.
Really, I've run into most of these problems so far and my solutions are in the library I linked. :-)
The alternative is to use a withKey argument that folds the two together, but then you're really just moving the conditional into the function.
Crell's problems do deserve a solution, either to make additional args get discarded, which is the JS way and makes a lot of sense, but it feels a bit too loose for my preference.... the only alternative would be something to explicitly swallow anything else
[1, 2, 3]->forEach(fn($x, ...) => $x * 2); which im not sure I like any more, even if it's technically accurate. It seems unnecessary.
I wouldn't have a method called forEach() at all. It's either a map or a filter or a reduce. Don't mess around with an amorphous "all of them and none".
99% of my foreach()s are either a map or a filter, and knowing which is which is helpful. It also allows for engine optimization potentially (like parallelizing operations).
Which, hey, if you want to swap out a synchronous map() for an async map built on filters, that's trivial with pipes. Not so much with scalar methods.
@IluTov One example is when you're iterating over a list calling a method on each item e.g. listeners.forEach(v => v.someMethod()); You might also be copying an array into a different datastructure e.g. values.forEach(v => someMap.set(v.id, v));
@Crell By trivial you mean you have to implement it yourself? :P Yes, that's another things, Swift and Rust have lazy interator APIs (operations are chained, and only run as you advance through the iterator) but honestly then we'd be better off just introducing better iterator classes.
So, (expr)->forEach(fn ($x) => $x->doFoo()) can always be rewritten as foreach (expr as $x) $x->doFoo();. I guess the former works slightly better if expr spans over multiple lines.
That syntax doesn't execute it. It technically creates a wrapping callable around the static method that you can invoke later.
On 7.4, the least-bad option is [RequestController::class, 'execute'] and pass that around as as a pseudo-callable, which works most of the time...
user17161735
18:31
ok look I'm working on this class: https://github.com/arcanisgk/swp/blob/master/src/Core/Router.php
this line of code to be more exact: https://github.com/arcanisgk/swp/blob/73e81c017d111f8a8cb992c87fd8d36eab307a19/src/Core/Router.php#L28
Everything that gets compiled ends up in this method: https://github.com/arcanisgk/swp/blob/73e81c017d111f8a8cb992c87fd8d36eab307a19/src/Core/Router.php#L97
and ends up calling this other class and method: https://github.com/arcanisgk/swp/blob/master/src/Core/RequestController.php
<?php
class Foobar {
public function zazify()
{
print "zazified!";
}
}
function leActivate(array $leCallable) {
(new $leCallable[0])->{$leCallable[1]}();
}
leActivate([Foobar::class, 'zazify']);
you can even curry args along to create the worse DIC you've ever thought of
user17161735
this is pretty magical: I was thinking of something more discoverable by the IDE, I'm using phpstorm.
@IluTov Yes I did, but will probably only update when I actually merge the PR
@Dharman Let's gooooooooooooo, and make this even more like functional prgramming languages :D
@Derick I've been working on a new autoloading mechanism for classes and functions, then one could add one for types so that one stops abusing classes, which IMHO is the main blocker atm for adding type aliases