Lost in the Generic Jungle

Greetings.


I am creating a framework and making heavy use of generics; something I am well used to after many years of expreience in C#, before coming to Swift when it was first launched.


I am getting an error on the declaration of the CommandSet class, which is proving difficult to resolve :


"Type 'CommandSet<subjectT, commandIdentifierT, executionContextT, commandT>' does not conform to protocol 'NotifyCommandChanged'"


This is accompanied by two "hints"


"Unable to infer associated type 'CommandType' for protocol 'NotifyCommandChanged'" on the declaration of the CommandType associatedtype in NotifyCommandChanged


"Inferred type 'commandT' (by matching requirement 'commandChanged') is invalid: does not inherit from 'Command<Self.SubjectType, Self.IdentifierType, Self.ExecutionContextType>'" on the commandChanged lazy var in CommandSet


I have been working on this for over a day now and would be very grateful if anyone out there can help me see what I am missing.


public protocol StringEnumType : Hashable, RawRepresentable where Self.RawValue == String { }

public protocol ExecutionContext { }

open class Command<subjectt, identifiert="" :="" stringenumtype,="" executioncontextt="" executioncontext="">
{
  public var text: String?
  
  public func execute() { }
}

public protocol EventArgs { }

public class Event<sendert, argst="" :="" eventargs="">
{
  private let sender: senderT
  
  public init(sender: senderT)
  {
    self.sender = sender
  }
  
  public func invoke(with args: argsT)
  {
  }
}

public struct CommandChangedEventArgs : EventArgs
{
  public let commandIdentifier: identifierT
  
  public let keyPath: AnyKeyPath
}

public typealias CommandChangedEvent<subjectt,
                                     identifierT : StringEnumType,
                                     executionContextT : ExecutionContext,
                                     commandT : Command<subjectt, identifiert,="" executioncontextt="">> = Event<commandset<subjectt, identifiert,="" executioncontextt,="" commandt="">, CommandChangedEventArgs>


public protocol NotifyCommandChanged
{
  associatedtype SubjectType
  
  associatedtype IdentifierType : StringEnumType
  
  associatedtype ExecutionContextType : ExecutionContext
  
  associatedtype CommandType : Command<subjecttype, identifiertype,="" executioncontexttype="">
  
  var commandChanged: CommandChangedEvent<subjecttype, identifiertype,="" executioncontexttype,="" commandtype=""> { get set }
}

open class CommandSet<subjectt,
                      commandIdentifierT : StringEnumType,
                      executionContextT : ExecutionContext,
                      commandT : Command<subjectt, commandidentifiert,="" executioncontextt="">> : NotifyCommandChanged
{
  private lazy var commands: [commandIdentifierT : commandT] = .init()
  
  public lazy var commandChanged: CommandChangedEvent<subjectt, commandidentifiert,="" executioncontextt,="" commandt=""> = .init(sender: self)
}

Replies

When I copy this in a project, I get a lot of syntax errors, such as:


Line 1: '=' must have consistent whitespace on both sides

and

Expected '>' to complete generic parameter list


Or line 16:

Use of undeclared type 'senderT'


Changing line 14:

public class Event<sendert, argst="" :="" eventargs="">

with

public class Event


clear those errors (others remain)



Did you post the real code ?