forked from studoverse/Kotlift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21_visibility.swift
105 lines (89 loc) · 1.79 KB
/
21_visibility.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Output:
Nested init
234
Nested init
com.moshbit.kotlift.Outer$Nested@4c5e43ee (or similar)
Nested init
5
78910348910
*/
// Uncommented code will not compile due to visibility modifiers
internal class Outer {
private let a = 1
internal let b = 2
internal let c = 3
let d = 4 // public by default
internal let n = Nested()
internal class Nested {
internal let e: Int32 = 5
init() {
print("Nested init")
}
}
private func o() -> Int32 {
return 6
}
internal func p() -> Int32 {
return 7
}
internal func q() -> Int32 {
return 8
}
func r() -> Int32 {
return 9
}
public func s() -> Int32 {
return 10
}
}
internal class Subclass: Outer {
// a is not visible
// b, c and d are visible
// Nested and e are visible
func printAll() {
// print(a)
print(b)
print(c)
print(d)
print(Outer.Nested())
print(Nested().e)
// print(o())
print(p())
print(q())
print(r())
print(s())
}
override init() {
}
}
public class Unrelated {
let o: Outer
// o.a, o.b are not visible
// o.c and o.d are visible (same module)
// Outer.Nested and Nested::e are not visible. In Swift they are visible, as there is no Protected.
func printAll() {
// print(o.a)
// print(o.b) // This statement runs in Swift, as there is no Protected.
print(o.c)
print(o.d)
/* // It is OK that the following 3 lines run in Swift:
val nested = Outer.Nested()
println(nested)
println(nested.e)*/
// print(o.o())
// print(o.p()) // This statement runs in Swift, as there is no Protected.
print(o.q())
print(o.r())
print(o.s())
}
init(o: Outer) {
self.o = o
}
}
func main(args: [String]) {
let x = Subclass()
x.printAll()
let y = Unrelated(o: x)
y.printAll()
}
main([])