Skip to content

Commit d97240e

Browse files
committed
Consolidated Cast operators for Array and Dictionary
1 parent 976f962 commit d97240e

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

Source/Array.swift

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,33 @@ public struct Array<T>
172172
}
173173

174174
//
175-
// Operators
175+
// Casts
176176
//
177177

178+
// Cast from/to native array
179+
178180
public static func __implicit(_ array: T[]) -> [T] {
179181
return [T](arrayLiteral: array)
180182
}
181183

182-
public static func __explicit(_ array: T[]) -> [T] {
183-
return [T](arrayLiteral: array)
184+
public static func __implicit(_ array: [T]) -> T[] {
185+
return array.nativeArray
184186
}
185187

188+
// Cast from/to platform type
189+
186190
public static func __implicit(_ list: PlatformList<T>) -> [T] {
187191
return [T](list)
188192
}
189193

190-
public static func __explicit(_ list: PlatformList<T>) -> [T] {
191-
return [T](list)
194+
public static func __implicit(_ array: [T]) -> PlatformList<T> {
195+
return array.platformList
192196
}
193197

194-
#if ISLAND && DARWIN
198+
// Darwin only: cast from/to Cocoa type
199+
200+
#if DARWIN
201+
#if ISLAND
195202
public static func __implicit(_ array: NSArray<T>) -> [T] {
196203
return List<T>(array)
197204
}
@@ -203,44 +210,33 @@ public struct Array<T>
203210
public static func __implicit(_ list: [T]) -> NSMutableArray<T> {
204211
return list.list.ToNSMutableArray()
205212
}
206-
#endif
207-
208-
#if COCOA
209-
public static func __implicit(_ list: PlatformImmutableList<T>) -> [T] {
210-
return [T](list)
211-
}
212-
213-
public static func __explicit(_ list: PlatformImmutableList<T>) -> [T] {
214-
return [T](list)
213+
#else
214+
public static func __implicit(_ array: [T]) -> PlatformImmutableList<T> {
215+
return array.platformList
215216
}
216217
#endif
218+
#endif
217219

218-
public static func __implicit(_ array: [T]) -> T[] {
219-
return array.nativeArray
220-
}
221-
222-
public static func __explicit(_ array: [T]) -> T[] {
223-
return array.nativeArray
224-
}
225-
226-
public static func __implicit(_ array: [T]) -> PlatformList<T> {
227-
return array.platformList
228-
}
220+
// Cocoa only: cast from/to different generic Cocoa type
229221

230-
public static func __explicit(_ array: [T]) -> PlatformList<T> {
231-
return array.platformList
222+
#if TOFFEE || DARWIN
223+
public static func __explicit<U>(_ array: NSArray<U>) -> [T] {
224+
return (array as! NSArray<T>) as! [T]
232225
}
233226

234-
#if COCOA
235-
public static func __implicit(_ array: [T]) -> PlatformImmutableList<T> {
236-
return array.platformList
227+
public static func __explicit<U>(_ array: [T]) -> NSArray<U> {
228+
return (array as! NSArray<U>) as! NSArray<U>
237229
}
238230

239-
public static func __explicit(_ array: [T]) -> PlatformImmutableList<T> {
240-
return array.platformList
231+
public static func __explicit<U>(_ array: [T]) -> NSMutableArray<U> {
232+
return (array as! NSMutableArray<T>) as! NSMutableArray<U>
241233
}
242234
#endif
243235

236+
//
237+
// Operators
238+
//
239+
244240
public static func + <T>(lhs: [T], rhs: [T]) -> [T] {
245241
var result = lhs
246242
for i in rhs.GetSequence() {

Source/Dictionary.swift

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,61 @@ public struct Dictionary<Key, Value> /*: INSFastEnumeration<T>*/
9999
}
100100

101101
//
102-
// Operators
102+
// Casts
103103
//
104104

105-
public static func __implicit(_ dictionary: PlatformDictionary<Key,Value>) -> [Key:Value] {
106-
return [Key:Value](dictionary)
107-
}
105+
// Cast from/to platform type
108106

109-
public static func __explicit(_ dictionary: PlatformDictionary<Key,Value>) -> [Key:Value] {
107+
public static func __implicit(_ dictionary: PlatformDictionary<Key,Value>) -> [Key:Value] {
110108
return [Key:Value](dictionary)
111109
}
112110

113111
public static func __implicit(_ dictionary: [Key:Value]) -> PlatformDictionary<Key,Value> {
114112
return dictionary.platformDictionary
115113
}
116114

117-
public static func __explicit(_ dictionary: [Key:Value]) -> PlatformDictionary<Key,Value> {
118-
return dictionary.platformDictionary
115+
// Darwin only: cast from/to Cocoa type
116+
117+
#if DARWIN
118+
#if ISLAND
119+
public static func __implicit(_ dictionary: NSDictionary<Key,Value>) -> [Key:Value] {
120+
return Dictionary<Key,Value>(dictionary)
121+
}
122+
123+
public static func __implicit(_ dictionary: [Key:Value]) -> NSDictionary<Key,Value> {
124+
return dictionary.dictionary.ToNSDictionary()
119125
}
120126

127+
public static func __implicit(_ list: [Key:Value]) -> NSMutableDictionary<Key,Value> {
128+
return Dictionary.dictionary.ToNSDictionary()
129+
}
130+
#else
131+
public static func __implicit(_ list: [Key:Value]) -> PlatformImmutableDictionary<Key,Value> {
132+
return Dictionary.platformFictionary
133+
}
134+
#endif
135+
#endif
136+
137+
// Cocoa only: cast from/to different generic Cocoa type
138+
139+
#if TOFFEE || DARWIN
140+
public static func __explicit<Key2,Value2>(_ dictionary: NSDictionary<Key2,Value2>) -> [Key:Value] {
141+
return (dictionary as! NSDictionary<Key,Value>) as! [Key:Value]
142+
}
143+
144+
public static func __explicit<Key2,Value2>(_ dictionary: [Key:Value]) -> NSDictionary<Key2,Value2> {
145+
return (dictionary as! NSDictionary<Key,Value>) as! NSDictionary<Key2,Value2>
146+
}
147+
148+
public static func __explicit<Key2,Value2>(_ dictionary: [Key:Value]) -> NSMutableDictionary<Key2,Value2> {
149+
return (dictionary as! NSMutableDictionary<Key,Value>) as! NSMutableDictionary<Key2,Value2>
150+
}
151+
#endif
152+
153+
//
154+
// Operators
155+
//
156+
121157
public static func + <T>(lhs: [Key:Value], rhs: [Key:Value]) -> [Key:Value] {
122158
var result = lhs
123159
for k in rhs.keys {
@@ -324,7 +360,7 @@ public struct Dictionary<Key, Value> /*: INSFastEnumeration<T>*/
324360
#endif
325361
}
326362

327-
public var keys: ISequence<Key> { // we deliberatey return a sequence, not an array, for efficiency and flexibility.
363+
public var keys: ISequence<Key> { // we deliberatey return a sequence, not an dictionary, for efficiency and flexibility.
328364
#if JAVA
329365
return dictionary.keySet()
330366
#elseif CLR || ISLAND
@@ -334,7 +370,7 @@ public struct Dictionary<Key, Value> /*: INSFastEnumeration<T>*/
334370
#endif
335371
}
336372

337-
public var values: ISequence<Value> { // we deliberatey return a sequence, not an array, for efficiency and flexibility.
373+
public var values: ISequence<Value> { // we deliberatey return a sequence, not an dictionary, for efficiency and flexibility.
338374
#if JAVA
339375
return dictionary.values()
340376
#elseif CLR || ISLAND

0 commit comments

Comments
 (0)