From: Jean Privat Date: Wed, 20 May 2015 00:30:31 +0000 (-0400) Subject: Merge: Safe collection access X-Git-Tag: v0.7.5~42 X-Git-Url: http://nitlanguage.org Merge: Safe collection access A proposition to solve an old API issue related to covariance in collections (and fix #1353) This PR updates the signatures of access methods in collections: `has`, `[]` and anything related that takes an `E` or a `K` in read-collections. This make these classes covariant-safe. Previously, the signature was to strictly accepts the bound, thus making the code unsafe but has the advantage to provide static hints ~~~nit assert [1,2,3].has("Hello") # static error, expected int got string ~~~ But this behavior had issues because: * unsafe code, but it is not because Nit *can* be unsafe that Nit *should* be unsafe. * a perfect semantic answer is possible, eq returning false in the previous example. thus the existing behavior is not POLA Because the philosophy of Nit is that static types should help the programmer without being annoying, this PR lift the constraint on parameters of all read access methods. The semantic is now more aligned with the one of dynamically typed language where the behavior is decided by messages and not by the types of things. This especially allows to use collections of thing when `==` does not implies the equality of static types (ex subclasses of Sequence). ~~~nit var a = [1,2,3] var l = new List[Int].from(a) assert a == l # by the law of Sequence var aa = [a] assert aa.has(l) # before the PR, error; with the PR, true var ma = new Map[Array[Int],String] ma[a] = "hello" assert ma.has_key(l) # before the PR, error; with the PR, true ~~~ Pull-Request: #1355 Reviewed-by: Alexis Laferrière Reviewed-by: Lucas Bajolet Reviewed-by: Alexandre Terrasa --- 2309d9308454a540fca94e325713c126c9082e8b