Re: [RFC:generics]
| From: | Rowan Collins | Date: | Fri, 29 Apr 2016 15:55:39 +0000 |
| Subject: | Re: [RFC:generics] | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92942@lists.php.net to get a copy of this message | ||
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"