Dominic Grostate wrote on 29/04/2016 16:45:
It's not insane, it's liskov substitution. But that is an insane
example, typical of Java to create a type that extends it's parent by
adding value constraints.. directly violating the principle.
Read the example again, and translate it into English:
function addPiToList(List<Number> $list): void
// "I am a function that accepts any list of numbers"
{
$list->add(3.1415);
// "To the list I was given, add the Double 3.1415, which is a kind of
Number"
}
$li = new List<Integer>;
// "This is a new list of Integers"
addPiToList($li)
// "You asked for a list of Numbers; Integers are Numbers, so I can give
you my list"
So far, so reasonable. But now lets see what gets executed:
$list->add(3.1415);
// "To the list of Integers you gave me, I will add the Double 3.1415"
// "Oops! A Double isn't allowed in your list, I will now produce a Type
Error"
Now go through the same with the amended List<? super Double> example:
function addPiToList(List<? super Double> $list): void
// "I am a function that accepts any list, whose members can include Double"
{
$list->add(3.1415);
// "To the list I was given, add the Double 3.1415"
}
$ln = new List<Number>;
// "This is a new list of Numbers, it can mix Integers and Doubles"
addPiToList($ln)
// "You asked for a list that can contain Integers; Integers are
Numbers, so I can give you my list"
So far, so reasonable. But now lets see what gets executed:
$list->add(3.1415);
// "To the list of Integers you gave me, I will add the Double 3.1415"
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php