blob: b5a3fd8af61234d326db736329a9f37f2f0d71bd [file] [log] [blame]
Michael Sullivan038f9252012-07-06 22:50:501// Tests that type assignability is used to search for instances when
2// making method calls, but only if there aren't any matches without
3// it.
4
Lindsey Kuper439afaa2012-07-31 17:27:515trait iterable<A> {
Michael Sullivan038f9252012-07-06 22:50:506 fn iterate(blk: fn(A) -> bool);
7}
8
Niko Matsakiseb0a34c2012-07-18 17:17:409impl vec<A> of iterable<A> for &[A] {
Michael Sullivan038f9252012-07-06 22:50:5010 fn iterate(f: fn(A) -> bool) {
11 vec::each(self, f);
12 }
13}
14
Niko Matsakiseb0a34c2012-07-18 17:17:4015impl vec<A> of iterable<A> for ~[A] {
Michael Sullivan038f9252012-07-06 22:50:5016 fn iterate(f: fn(A) -> bool) {
17 vec::each(self, f);
18 }
19}
20
21fn length<A, T: iterable<A>>(x: T) -> uint {
22 let mut len = 0;
23 for x.iterate() |_y| { len += 1 }
Brian Andersonb3559362012-08-02 00:30:0524 return len;
Michael Sullivan038f9252012-07-06 22:50:5025}
26
27fn main() {
28 let x = ~[0,1,2,3];
29 // Call a method
30 for x.iterate() |y| { assert x[y] == y; }
31 // Call a parameterized function
32 assert length(x) == vec::len(x);
33 // Call a parameterized function, with type arguments that require
34 // a borrow
35 assert length::<int, &[int]>(x) == vec::len(x);
36
37 // Now try it with a type that *needs* to be borrowed
38 let z = [0,1,2,3]/_;
39 // Call a method
40 for z.iterate() |y| { assert z[y] == y; }
41 // Call a parameterized function
42 assert length::<int, &[int]>(z) == vec::len(z);
43}