forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested-protocols.swift
64 lines (45 loc) · 1.33 KB
/
nested-protocols.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
enumE{
protocolP{}
}
structConforms{}
extensionConforms:E.P{}
structDoesNotConform{}
structS<T>{}
extensionS:E.Pwhere T:E.P{}
@inline(never)
func castToNested(_ value:Any)->(anyE.P)?{
value as?anyE.P
}
// Regular conformance.
precondition(castToNested(Conforms())!=nil)
precondition(castToNested(DoesNotConform())==nil)
// Conditional conformance.
precondition(castToNested(S<Conforms>())!=nil)
precondition(castToNested(S<DoesNotConform>())==nil)
// Verify that 'E.P' and a non-nested 'P' are different.
protocolP{}
@inline(never)
func castToNonNested(_ value:Any)->(anyP)?{
value as?anyP
}
precondition(castToNonNested(Conforms())==nil)
precondition(castToNonNested(S<Conforms>())==nil)
precondition(castToNonNested(DoesNotConform())==nil)
precondition(castToNonNested(S<DoesNotConform>())==nil)
// Local protocols.
@inline(never)
func f0(_ input:Any)->(Any, inputConforms:Bool){
protocol LocalProto {}
structConformsToLocal:LocalProto{}
iflet input = input as?anyLocalProto{
return(input,true)
}else{
return(ConformsToLocal(),false)
}
}
varresult=f0(DoesNotConform())
precondition(result.inputConforms ==false)
result =f0(result.0)
precondition(result.inputConforms ==true)