[$login, $password] = disperse( // could throw CompositeException
[Login::fromString(...), $loginString], // could throw LoginValidationFailedException
[Password::fromString(...), $passwordString], // could throw WeakPasswordException
);
function disperse(array ...$tasks): array
{
foreach ($tasks as [$fn, ...$args]) {
try {
$results[] = $fn(...$args);
} catch (\Throwable $e) {
$errors[] = $e;
}
}
if ($errors) {
throw new CompositeException($errors);
}
return $results;
}
Naive implementation is: