forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeach.swift
39 lines (32 loc) · 1.18 KB
/
foreach.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// RUN: %target-parse-verify-swift
struct IntRange<Int> : SequenceType, GeneratorType {
typealias Element = (Int, Int)
func next() -> (Int, Int)? {}
typealias Generator = IntRange<Int>
func generate() -> IntRange<Int> { return self }
}
func for_each(r: Range<Int>, iir: IntRange<Int>) {
var sum = 0
// Simple foreach loop, using the variable in the body
for i in r {
sum = sum + i
}
// Check scoping of variable introduced with foreach loop
i = 0 // expected-error{{use of unresolved identifier 'i'}}
// For-each loops with two variables and varying degrees of typedness
for (i, j) in iir {
sum = sum + i + j
}
for (i, j) in iir {
sum = sum + i + j
}
for (i, j) : (Int, Int) in iir {
sum = sum + i + j
}
// Parse errors
for i r { // expected-error 2{{expected ';' in 'for' statement}} expected-error {{use of unresolved identifier 'i'}}
}
for i in r sum = sum + i; // expected-error{{expected '{' to start the body of for-each loop}}
for let x in 0..<10 {} // expected-error {{'let' pattern is already in an immutable context}} {{7-11=}}
for var x in 0..<10 {} // expected-error {{Use of 'var' binding here is not allowed}} {{7-11=}}
}