Default value for general default constraint

Consider the following code:

protocol JSONParserType {
    associatedtype Element
}

// MARK: - Entities
struct Item {}

// MARK: - Parsers
struct OuterParser<T: JSONParserType where T.Element == Item>: JSONParserType {
    typealias Element = Item
    let innerParser: T

    init(innerParser: T = InnerParser()) {
        self.innerParser = innerParser
    }
}

struct InnerParser: JSONParserType {
    typealias Element = Item
}

OuterParserhas a child parser that must be limited to a specific type. Unfortunately, providing a default value in the initializer (or in the property definition itself) causes the compiler to throw "The default argument value of type" InnerParser "cannot be converted to type" T ".

If I remove the default assignment and just create an instance OuterParser, providing InnerParserexplicitly, everything is fine.

let outerParser = OuterParser(innerParser: InnerParser())

My question is because the reason the approach providing a default value that really meets the constraints does not work.

+4
source share
2

, T - , . , - ( ). InnerParser T, T , InnerParser.

, , :

struct AnotherParser: JSONParserType {
    typealias Element = Item
}

, . , , :

let parser = OuterParser<AnotherParser>()

generic AnotherParser, InnerParser ( AnotherParser). , .

, :

struct OuterParser<T: JSONParserType where T.Element == Item>: JSONParserType {
    typealias Element = Item
    let innerParser: T

    init() {
        self.innerParser = InnerParser()
    }

    init(innerParser: T) {
        self.innerParser = innerParser
    }
}

, T , InnerParser. , downcast T - , .

, . , factory OuterParser.

enum Parser {
    static func createParser() -> OuterParser<InnerParser> {
        return OuterParser(innerParser:InnerParser())
    }
    static func createParser<T>(innerParser:T) -> OuterParser<T> {
        return OuterParser(innerParser:innerParser)
    }
}

let innerParser = Parser.createParser() // OuterParser<InnerParser>

let anotherParser = Parser.createParser(AnotherParser()) // OuterParser<AnotherParser>

, .

Swifty, , , , .

+1

enter image description here

type T child protocol of JSONParserType, :

init(innerParser: T = InnerParser() as! T) {
    self.innerParser = innerParser
}
0

All Articles