« first day (3774 days earlier)      last day (1171 days later) » 

12:02 AM
> Method parameters have modifiers available to change the desired outcome of how the parameter is treated. Each method has a specific use case:

1. `ref` is used to state that the parameter passed may be modified by the method.
2. `in` is used to state that the parameter passed cannot be modified by the method.
3. `out` is used to state that the parameter passed must be modified by the method.
 
@LeviMorrison no idea how in would be even implemented in PHP … you could create some weird boxing… but eih…
 
We can ignore in, that was just in the quoted source.
The way out is worded makes it a bit interesting for things that may conditionally modify, which makes it sound more like ref should be used in such cases.
 
I don't think out should require modification, but, if not modified, and accessed afterwards, it should be undef.
and emit the appropriate warning
 
Based on this statement and that one, how are inout and out any different in your opinion?
Just that accessing the value of a variable that is out for anything except a write is an error too?
 
well, out does notably not pass the initial value to the function, but the value starts as IS_UNDEF in the function
inout does not reset
 
12:11 AM
I beg you, add an HokieCokieException for errors in it.
 
@bwoebi I'm struggling to think of examples where it really matters.
 
12:26 AM
Could always just mandate an initializer... although I guess that might lead to odd behaviour
 
12:37 AM
@LeviMorrison It's mostly an indicator by the function "I will write to this - escape analysis can know that the input is not going to be processed further"
 
1:07 AM
@LeviMorrison IMHO foo(out string $ref) should type check that at the end of the func call $ref is a string
Currently by-ref arguments are type check on function entry but not exit
 
true
 
Do any languages that have out parameters support them being optional? Because if that was possible, it might avoid long arguments about whether certain errors should be exceptions or not:
function foo(out $error) {
	[$success, $validation_errors] = bar();

	if ($success === true) {
		return true;
	}

	if (is_set($error) == true) {
		$error = new ValidationError($validation_errors);
		return false;
	}

	throw new new ValidationError($validation_errors);
}


foo($error); // This sets error when validation problem occurs
foo(); // This throws error
as the caller could then decide whether they want to handle the error locally, or have the exception thrown if they choose to, or forget to handle it.
 
I think it should be optional
or maybe…
Well, the point is… what guarantees do we want to make?
maybe we should have out? $error and out $error
in former case it may sometimes not be defined - in latter case it's checked whether it's undef at function end
ah
misread
@Danack but … how would you skip that arg? this basically requires the arg being last
there then needs to be a special placeholder for that (like passing a literal null to an out value)
 
1:36 AM
Not the best idea iin the world IMO to have such behaviour determined by an arg like that (the out error deciding if it throws or not)
But function foo(out int $error = 0) { ... } would always have a value set
although class return types would be a bit of a hassle. Are you thinking a TypeError would be thrown if the out param didn't match the type specified on return
 
 
7 hours later…
9:03 AM
@ breaks functions ・ Scripting Engine problem ・ #80750
 
9:21 AM
@Jeeves looooooool
 
morns
 
9:48 AM
@bwoebi sure. Requiring it to be null (so foo(null);) would be fine, and make it more of an explicit choice for the person writing the calling code.
@MarkR why not? it allows the calling code to choose between "I can and want to handle the error locally, without having to write a try/catch block" and "I can't handle the error locally, so it needs to be passed back up the call stack, for which exceptions are pretty good".
And more importantly, it avoids all of the arguments people have for functions where either throwing an exception, or returning an error that needs to be checked, are both reasonable positions to take.
 
 
4 hours later…
1:38 PM
Making something needlessly complicated or harder to understand to avoid arguments is something i'd be happy have an argument about being a bad idea.
Two arguments even
 
Refactoring states two methodologies, "extract method from argument" is one, the converse methodology that I don't remember offhand is adding an argument to reduce complexity. It varies based on the problem.
 
1:58 PM
Uncle Bob says that more arguments add more complexity, no arguments is a good number, more than three and you are doing something wrong.
 
I'm all for spamming arguments, but differing behaviours should be different functions. An OUT arg that assigns an exception rather than throwing it takes that to ridiculous levels... Not only do you have an extra argument, but now you have to have some kind of union return type, and you've introduced a value that it's not clear where it came from without inspecting the receiver.
and all you've done is appeased people who are still stuck in the ways of C, at the cost of making it more complicated for literally everyone else. Forever.
 
 
1 hour later…
3:10 PM
Comma in recipient name breaks email delivery ・ Mail related ・ #80751
 
 
2 hours later…
5:29 PM
@Girgias inout should probably check at both.
That's probably a more important difference between inout and out than the reading aspect.
@NikiC For bidirectional iterators to be efficient I'll have to change internal iterator structs, which indicates the base interface shouldn't be in the Spl, but core language.
So something like:
\BidirectionalIterator, \Spl\BidirectionalArrayIterator, and \Spl\ReverseIterator?
This is a lot more work overall. There are a few types that may benefit from it, so it may be worth it, but I'm not loving the extra work since I'm only contributing in spare time on this one.
@Danack That has been solved in other languages by using a result type, which is either an ok result or an error. If you directly reach into the ok result (like a .get()) and it's not an ok, then it will throw for you (so you can focus writing the happy path).
But if people don't want to throw, they can check it with .is_ok() or whatever color you want to paint that bike shed.
 
5:47 PM
@Danack That's basically what Go does, and you return nil for either the success or error case, as appropriate. It's one of the most-complained about features of Go. :-)
 
6:03 PM
@LeviMorrison Does the stack trace appear from the original call site, or from within the .get ?
 
6:28 PM
@MarkR Suppose that depends somewhat on the language. Does PHP do it today at the point of throw or the point of creation?
Theoretically you can throw a new exception and link the previous one, but that may be harder to deal with because people will want to preserve the exception type. It may be a good use of a clone handler (clone the exception, update its stack and link to the previous one).
 
7:02 PM
@LeviMorrison They should yeah
 
@LeviMorrison the bit that's different is that the calling code, and the function being called can communicate the two different scenarios "I can and want to handle the error locally, without having to write a try/catch block" and "I can't handle this, just throw an exception"
@Crell and yeah.....it's the main thing I don't like about Go. Having to pass errors back up the stack by hand is so annoying.....it's appropriate for things like banking applications (where every error needs to be considered at every level), but for things like "resize an avatar image" that fails due to temp disk space running out on that particular server...no-one cares.....just get the user to refresh the page.
 
I want to do a blog post or book or presentation or something on error handling some time. It's a fascinating subject that gets nowhere near as much attention as it should.
 
What do you guys think, for single line function declarations/calls should the last parameter have a trailing comma? Eg. function foo($bar, $baz,) { \n ... \n }
Or only when the parameters are broken into more than one line
 
I'd say only on multiple lines.
 
function foo(
    $bar,
    $baz,
    $qux,
    $quux,
) {
   ...
}
 
7:11 PM
Yes.
 
That makes sense to me
I also just learned what goes after baz
foobar, foo, bar, baz, qux, quux, quuz, corge, grault, garply, waldo, fred, plugh, xyzzy, and thud
 
stare.gif
 
@scorgn Follow PSR-1, PSR-2 and PSR-12.
 
@Tpojka That doesn't take into account PHP 8 though
 
Maybe I misread question. I'd say in general matter, when you use multiple lines than - yes to trailing comma. My previous response could say when to use multiple lines. Should trailing comma be used in single line arguments - up to you. I wouldn't, probably.
 
7:19 PM
@Tpojka Ah right. Yeah I agree not to do so in single line arguments - looks weird and doesn't have any real advantage
 
@scorgn Only bits of distraction as comma in same line before closing parentheses.
 
@scorgn if it fits on a single line it doesn't need a trailing....but your examples are missing parameter types...which usually makes the parameters not fit on a single line.
 
@Danack True
 
@Crell Can I get you to review my PR for the docs about error handling? :p
 
7:44 PM
I have it sitting open in a tab here somewhere...
 
That's good to know :)
 
8:07 PM
@Girgias So... here's a tricky problem. You're moving exceptions into control flow. But... "exceptions are not for flow of control" is a regular drum-beat, and one I agree with. Yet they do change the flow of the program.

This is a tricky question.
 
I do agree that they shouldn't be over-used for control flow, but they are, and that section has return so
I feel that section should also have yield and yield from instead of this being hidden away in the Generator docs, but that's something separate
 
 
3 hours later…
10:46 PM
php.net/manual/en/reflectionparameter.getclass.php - OK, it's deprecated. What are we supposed to use instead?
 
11:07 PM
@Crell getType()
 
Oh good, 3v4l just broke.
Ugh. So it's now ->getType()->getName() to get the class that a parameter is for.
Also, PHPStorm seems to think getType() returns void. :-)
 

« first day (3774 days earlier)      last day (1171 days later) »