in Education by
Here is what I want to do: protocol GenericFactory { associatedtype Input associatedtype Value func create(with input: Input) -> Value } class Base { var input: Factory.Input { preconditionFailure("To be overriden") } } protocol ValueProtocol {} struct SomeInputImpl {} protocol ValueFactory: GenericFactory where Input == SomeInputImpl, Value: ValueProtocol {} class Child: Base { override var input: Factory.Input { return SomeInputImpl() } } On the line swift override var input: Factory.Input { I'm getting an error Property 'input' with type 'SomeInputImpl' cannot override a property with type 'Factory.Input'. I do not understand the reason of this error as the type of input is described in ValueFactory protocol, but because of some reasons swift interprets the Factory.Input in base and Factory.input in Child as different types. Can someone please explain me what's wrong here? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Just to complete the point about this being addressed in Swift 5, here is the demonstration that will work in Swift 5, but not in Swift 4.2, given your example code. struct Val: ValueProtocol {} struct VF: ValueFactory { func create(with input: SomeInputImpl) -> Val { return Val() } } let child = Child() child.input // SomeInputImpl But you would almost certainly be better off using functions rather than GenericFactory. The main reason for the Factory pattern is to deal with languages that lack higher-order functions and first-class types, such as pre-8 Java. In languages with higher-order functions and first-class types (like Swift), the Factory pattern is typically unnecessary. You can just pass the function (Input) -> Value directly. Your example doesn't show the caller, so it's hard to know precisely what function-based solution will work best, but in most cases, generic functions are a much better approach than protocols+associatedType+generics+inheritance. In particular, mixing class inheritance into associated types tends to get you into bad corners (because associated types and class inheritance are somewhat orthogonal approaches to polymorphism).

Related questions

0 votes
    Which of these type parameters is used for a generic class to return and accept any type of object? (a) K ... & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic class to return and accept a number? (a) K (b) N ... Interfaces & Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Without getting into the details of why, I'm looking for a clean (as possible) way to replace ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    I am trying to override the tomcat session manager with an embedded tomcat. These are the steps preformed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept any type of object? (a) ... chapter Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept a number? (a) K (b) N ... in portion Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Which of these types cannot be used to initiate a generic type? (a) Integer class (b) Float class (c) ... in section Generics of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    I've to solve a problem regarding generics which gives me quite the stomache ache. Basically it's supposed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I've to solve a problem regarding generics which gives me quite the stomache ache. Basically it's supposed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of these type parameters is used for a generic methods to return and accept a number? (a) K ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
...