On 6 January 2016 at 18:55, Ryan Pallas <derokor...@gmail.com> wrote:
> Hi Dan,
>

> having the function name as a string will make it harder to find in an IDE
> since go to definition won't work (and since functions aren't first class
> citizens as in javascript, we can't pass a raw name AFAIK)

Yes, this is a limition of PHP currently. As it does keep coming up I
think this is something that needs to be addressed, but would need to
be done in a separate RFC.

For this particular case, I don't think it should be a problem. The
IDE should be able to know that the handler is a callable as that is
the only thing that would be allowed there.

> And what is the benefit of having either of these options being part of the 
> language

Er......I don't think implementing it in userland like you sugggest is
workable. It would mean  calling code would look like:

tryAndSuppress('mkdir', ['/path/to/some', 0755, true], ['IOException']);
$fh = tryAndSuppress('fopen', ['/path/to/some/file.ext', 'r'],
['FileNotFoundException']);

for almost all code, which looks horrible, and breaks all static analysis tools.

> in one place I need to suppress FileException and somewhere
> else I don't want to.

If/when I write this up as an RFC, I'm going to suggest having two
built-in functions that make it easy and safe to do the normal things
that code does over and over again; convert a generic exception type
to a more specific one, and suppress certain exceptions. See below for
outline implementations of those two functions.

> Someone comes along and changes the fooRaiseException
> function and now the place where we need to know about FileException we no
> longer do.

I know what you mean, but the same is true of all code that is not
called directly. For example in routing for HTTP applications, the
routes are setup and then called dynamically. Yes, if someone changes
what the code does in the callable, then what the application does
changes. Additionally with the 'suppress' and 'convert' function
below, then most people shouldn't need need to use custom functions.

But also, in general, I think it's a mistake to limit the power of a
programming langauge to make in universally safe to use. At least to
some extent, if a feature can never be misused, then it's probably not
powerful enough. Stuff needs to be safe to use by default, but if
people want to do crazy stuff, we shouldn't delibrately stop them aka


“You can't give her that!' she screamed. 'It's not safe!'
IT'S A SWORD, said the Hogfather. THEY'RE NOT MEANT TO BE SAFE.
'She's a child!' shouted Crumley.
IT'S EDUCATIONAL.
'What if she cuts herself?'
THAT WILL BE AN IMPORTANT LESSON.”
― Terry Pratchett, Hogfather


cheers
Dan

This is some example code, that uses the suppress and convert
functions below to suppress any IOException raised by mkdir, and then
convert and IOException generated by file_put_contents into a
SavingDataException

function saveData($directoryName, $data) {
    // If the directory already exists, that is
    mkdir($directoryName, 0755, true) raise suppress("IOException");

    // Actually write the data. Failure to save gets converted
    // into a SavingDataException
    file_put_contents($directoryName."/foo.txt", $data) raise
convert("SavingDataException", "IOException");
}


// Returns a closure that suppresses any exception that matches
// the listed types.
function suppress(...$suppressedExceptions) {
    $fn = function (\Throwable $t) use ($suppressedExceptions) {
        foreach ($suppressedExceptions as $suppressedException) {
            if (is_a($t, $suppressedException) === true) {
                return;
            }
        }
        throw $t;
    };

    return $fn;
}

// Returns a closure that converts all exceptions that are listed
// into a new exception of the type '$newException'Type with the previous
// exception set as the 'previous exception'.
function convert($newExceptionType, ...$exceptionTypes) {
    $fn = function (\Throwable $t) use ($exceptionTypes) {
        foreach ($exceptionTypes as $exceptionType) {
            if (is_a($t, $exceptionType)) {
                throw new $newExceptionType(
                    $t->getMessge(),
                    $t->getCode(),
                    $t
                );
            }
        }

        throw $t;
    };

    return $fn;
}

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to