Why isn’t associated type inference working here?

i have three protocols that look something like this:

extension Grammar 
{
    typealias Parsable = _GrammarParsable
}
protocol _GrammarParsable 
{
    associatedtype Terminal 
    ...
}
extension Grammar 
{
    typealias Serializable = _GrammarSerializable
}
protocol _GrammarSerializable 
{
    associatedtype Terminals where Terminals:Sequence  
    
    var serialized:Terminals 
    {
        get 
    }
}
protocol _GrammarTerminalSequence:Grammar.Parsable, Grammar.Serializable
    where Terminals.Element == Terminal, Terminal:Equatable 
{    
    static 
    var terminals:Terminals
    {
        get 
    }
    
    init()
}
extension Grammar.TerminalSequence 
{
    var serialized:Terminals 
    {
        Self.terminals
    }
}

now, i try to add some types conforming to Grammar.TerminalSequence:

extension Character
{
    struct Colon:Grammar.TerminalSequence
    {
        static 
        let terminals:CollectionOfOne<Character> = .init(":")
    }
}

but then i just end up with

grammar/grammar.swift:71:12: error: type 'Character.Colon' does not conform to protocol '_GrammarTerminalSequence'
    struct Colon:Grammar.TerminalSequence
           ^
grammar/grammar.swift:71:12: error: type 'Character.Colon' does not conform to protocol '_GrammarTerminalSequence'
    struct Colon:Grammar.TerminalSequence
           ^
grammar/grammar.swift:74:13: note: candidate has non-matching type 'CollectionOfOne<Character>'
        let terminals:CollectionOfOne<Character> = .init(":")

instead, i have to add all the boilerplate typealiases to make it compile:

    struct Colon:Grammar.TerminalSequence
    {
        typealias Terminal  =                 Character
        typealias Terminals = CollectionOfOne<Character> 
        static 
        let terminals:CollectionOfOne<Character> = .init(":")
    }

i am using nearly a hundred of these “TerminalSequence” types. why can’t swift infer the Terminals and Terminal types from the terminals:CollectionOfOne<Character> property?

Terms of Service

Privacy Policy

Cookie Policy