-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpaqueTypes_Part1.swift
78 lines (51 loc) · 1.52 KB
/
OpaqueTypes_Part1.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
protocol Cookable: Equatable {
var item: [String] {
get
}
}
struct Vegetable: Cookable {
var item: [String] = ["Tomato", "Brinjal", "Potato"]
}
func display() -> some Cookable {
return Vegetable()
}
var temp = display()
print(type(of: temp)) // Vegetable
//--------------------------------------
protocol Edible {
associatedtype type
var names: [type] {
get
}
}
struct Fruits<type>: Edible {
var names: [type]
}
//An 'opaque' type must specify only 'Any', 'AnyObject', protocols, and/or a base class
//func toDisplay(names: [String]) -> Collection { // error - Protocol 'Collection' can only be used as a generic constraint because it has Self or associated type requirements
//doc: "You can’t use Container as the return type of a function because that protocol has an associated type."
func toDisplay<T>(names: [T]) -> some Collection {
let names2 = names[0...1]
//return names2[0] // String
//return Set(names2) // Set<String>
return names2 //ArraySlice<String>
}
var fruit1 = Fruits(names: ["Apple", "Orange", "Mango"])
var temp1 = toDisplay(names: fruit1.names)
print(type(of: temp1)) //ArraySlice<String>
//-----------------------------------------------
class Person {
var name: String = "abc"
}
class Employee1: Person {
var id: Int = 100
}
class Employee2: Person {
var id: String = "200"
}
func showType() -> some Person {
return Employee2()
}
var type1: Person = Employee2()
var temp2 = showType()
print(type(of: temp2))