Create an array of protocols with constrained associated types - swift

This is a basic example of creating an array of protocols with associated types using type erasure:
protocol ProtocolA {
associatedtype T
func doSomething() -> T
}
struct AnyProtocolA<T>: ProtocolA {
private let _doSomething: (() -> T)
init<U: ProtocolA>(someProtocolA: U) where U.T == T {
_doSomething = someProtocolA.doSomething
}
func doSomething() -> T {
return _doSomething()
}
}
Creating an array of them isn't hard:
let x: [AnyProtocolA<Any>] = []
Is there any way way I can create an array of protocols which have associated types that are constrained? This is what I have tried:
protocol Validateable {
// I removed the functions and properties here to omit unreleveant code.
}
protocol ProtocolA {
associatedtype T: Validateable
func doSomething() -> T
}
struct AnyProtocolA<T: Validateable>: ProtocolA {
private let _doSomething: (() -> T)
init<U: ProtocolA>(someProtocolA: U) where U.T == T {
_doSomething = someProtocolA.doSomething
}
func doSomething() -> T {
return _doSomething()
}
}
It compiles! But didn't it defeated the chance of creating an array of AnyProtocolA's now? Now I can't use type Any as a placeholder in my array.
How do I create an array of AnyProtocolA's which has a constrained associated type? Is it even possible? This won't work since Any ofcourse doesn't conform to Validateable:
let x: [AnyProtocolA<Any>] = []
Extending Any can't be done:
extension Any: Validateable {} // Non nominal type error
Edit:
I think I already found it, just type erasure the protocol Validateable as well:
protocol Validateable {
// I removed the functions and properties here to omit unreleveant code.
}
protocol ProtocolA {
associatedtype T: Validateable
func doSomething() -> T
}
struct AnyProtocolA<T: Validateable>: ProtocolA {
private let _doSomething: (() -> T)
init<U: ProtocolA>(someProtocolA: U) where U.T == T {
_doSomething = someProtocolA.doSomething
}
func doSomething() -> T {
return _doSomething()
}
}
struct AnyValidateable<T>: Validateable {}
Now I can use it as:
let x: [AnyProtocolA<AnyValidateable<Any>>] = []
Any answers that are better are always welcome :)

Related

How to use a protocol function as block definition?

Here a function b calls a and passes a completion block:
func a(completion: (_ succeeded: Bool)) {
completion(true)
}
func b() {
a() { succeeded in
...
}
}
Can I use a protocol function definition to define the block?
Something like this (which does not work):
protocol P {
func c(succeeded: Bool)
}
func a(completion: P) {
completion.c(succeeded: true)
}
func b() {
a() { c(succeeded) in
...
}
}
Note: I am not looking for the protocol/delegate concept.
The issue with using a protocol here is that functions cannot conform to a protocol, only classes, enums and structs can conform to a protocol. So you either need one of these types or an instance of one of these types, but then the instance seems superfluous. If you really want to do it the protocol way here is an example with static conformance on an enum:
import UIKit
protocol Callable {
static func call(someString: String, someInt: Int)
}
enum MyCallable: Callable {
static func call(someString: String, someInt: Int) {
print(someString, someInt)
}
}
func someFunction<T: Callable>(callable: T.Type) {
callable.call(someString: "a",someInt: 1)
}
someFunction(callable: MyCallable.self)
You can use a labeled tuple to give yourself argument labels if that is what you are after. Here is a playground example:
import UIKit
typealias MyArguements = (someInt: Int, someString: String)
func someFunction( completion: #escaping (MyArguements) -> Void) {
completion((someInt: 1, someString: "a"))
}
someFunction { tuple in
let (someInt, someString) = tuple
print(someInt, someString)
}

Swift generic without force unwrap downcast

I try to create some generic based code:
protocol ViewModelsCreator {
associatedtype T: EditItemViewModelType
func editItemViewModel<T>() -> T
}
class PlacesListViewModel: ViewModelsCreator {
typealias T = EditPlaceViewModel
func editItemViewModel<T>() -> T {
return EditPlaceViewModel()
}
}
class EditPlaceViewModel: EditItemViewModelType {}
protocol EditItemViewModelType {}
The playground shows error:
cannot convert return expression of type 'EditPlaceViewModel' to
return type 'T'
and suggest to use
return EditPlaceViewModel() as! T
Is there any solution to avoid this (as! T) force unwrap code? I think compiler should figure out that EditPlaceViewModel is EditItemViewModelType and should satisfy this generic.
You need to remove the <T> in the ViewModelsCreator protocol and the PlacesListViewModel class.
protocol ViewModelsCreator {
associatedtype T: EditItemViewModelType
func editItemViewModel() -> T
}
class PlacesListViewModel: ViewModelsCreator {
typealias T = EditPlaceViewModel
func editItemViewModel() -> T {
return EditPlaceViewModel()
}
}
You can also remove the typealias and replace -> T with -> EditPlaceViewModel in the PlacesListViewModel class. It works either way but this is more explicit.
class PlacesListViewModel: ViewModelsCreator {
func editItemViewModel() -> EditPlaceViewModel {
return EditPlaceViewModel()
}
}

Nested generics in a swift function

Assume I have this bit of code:
protocol MyProtocol {
}
struct MyStruct: MyProtocol {
}
class MyClass<P: MyProtocol> {
// Required for compiling
required init() {
}
}
class MySpecialClass: MyClass<MyStruct> {
}
func foo<P: MyProtocol, C: MyClass<P>>(protocolType: P.Type) -> C {
return C()
}
This compiles and calling
let specialClass: MySpecialClass = foo(protocolType: MyStruct.self)
creates an instance of type MySpecialClass.
What I would like is to not have to pass in the type of P so that I could simply call
let specialClass: MySpecialClass = foo()
and P would be automatically deduced.
Is there any way to make this happen?
As #Hamish already mentioned,
func foo<P, C: MyClass<P>>() -> C {
return C()
}
let specialClass: MySpecialClass = foo()
works in Swift 4. The Swift 3 compiler however complains that
error: generic parameter 'P' is not used in function signature
and a possible workaround is to add a dummy parameter of type
P? with a default value:
func foo<P, C: MyClass<P>>(dummy: P? = nil) -> C {
return C()
}
let specialClass: MySpecialClass = foo()
I believe that this will work:
protocol MyProtocol {
}
struct MyStruct: MyProtocol {
}
class MyClass<P: MyProtocol> {
// added a typealias for P
typealias ContainedProtocol = P
// Required for compiling
required init() {
}
}
class MySpecialClass: MyClass<MyStruct> {
}
// added a default value for protocolType
func foo<P, C: MyClass<P>>(protocolType: P.Type = C.ContainedProtocol.self) -> C {
return C()
}
let specialClass: MySpecialClass = foo()
By adding a typealias for P in MyClass<P: MyProtocol> we can refer to that type. We then set it as a default value in foo<P, C: MyClass<P>>(protocolType:) -> C. The compiler can then deduce P from that information.
Martin R’s version seems to work about as well as mine so go with whichever you like best. His seems a bit cleaner as it doesn’t need a typealias and it is fairly succinct.
Here’s his (it was a comment, it’s now an answer):
func foo<P, C: MyClass<P>>(dummy: P? = nil) -> C { ... }

Swift - How to get class that extended from protocol?

Is there a way that I could get the Class type or Struct type that extended my Protocol?
Here are my sample code:
protocol a {}
extension a {
static func list(completion: ([StructType] -> Void)) {
var items = [StructType]()
...
completion(items)
}
}
struct b{}
extension b: a {}
struct c{}
extension c: a{}
In this case I want to dynamically get the type of struct a and b, so that I could generate a list of it and return.
Thank you in advance for kindly answering my question.
Use the Self keyword
protocol P {
init()
}
extension P {
static func list(completion: ([Self]) -> Void) {
let items = [Self(), Self(), Self()]
print(Self.self)
completion(items)
}
}
struct B {}
extension B: P {}
class C {
required init() {}
}
extension C: P {}
B.list{ print($0) }
C.list{ print($0) }

Using array of protocol in Swift class' generics

Is there any way to use array of protocol's generic?
For example,
/* I want to use protocol like below,
* but I can't because protocol is not concrete
* so cannot make array of it */
class MyClass<T where T:MyProtocol, T:[MyProtocol]> {
let result: T
}
protocol MyProtocol {
init(with: String)
}
class SpecialThing: MyProtocol {
let appleWatch: AppleWatch
init(with: String) {
self.appleWatch = AppleWatch(with)
}
}
class SampleClass {
func test {
typealias listCallback = (MyClass<[SpecialThing]>, NSError) -> ()
typealias oneCallback = (MyClass<SpecialThing>, NSError) -> ()
}
}
There can be one object or array of protocol's subclass.
I think "typealias" does not help me.
I want to find something more simple way.....
My first issue with this is that the type signature is wrong:
class MyClass<T where T:MyProtocol, T:[MyProtocol]>
That's the same type of thing as doing:
let t: String
let t: [String]
t = String("foo")
The compiler will complain because you are redefining T, once as a MyProtocol and again as an array of MyProtocol. You can't have both, you can only have one.
Answer: Use a construct like Either:
enum Either<T, U>
{
case Left(T)
case Right(U)
var leftValue: T?
{
if case .Left(let leftValue) = self
{
return leftValue
}
return nil
}
var rightValue: U?
{
if case .Right(let rightValue) = self
{
return rightValue
}
return nil
}
}
Allowing for:
class MyClass<T: MyProtocol>
{
let result: Either<T, [MyProtocol]>
}