Generic typealias vs generic subclass

Here's a weird one.

This compiles :

public class BaseListCommand<itemT : NotifyPropertyChanged & Equatable, contextT, listT> : Command
{
}

public typealias ListCommand<itemT : NotifyPropertyChanged & Equatable> = BaseListCommand<itemT, Array<itemT>, Array<itemT>>

This doesn't

public class ListCommand<itemT : NotifyPropertyChanged & Equatable, contextT : Array<itemT>, listT : Array<itemT>>
{
  
}

And yet the typealias gives me the same signature

Reduced and including the error message:

class A_<I: Equatable, Context, List> {}
typealias A<I: Equatable> = A_<I, Array<I>, Array<I>>

class B<I: Equatable, Context: Array<I>, List: Array<I>> {}

/*
main.swift:8:30: error: inheritance from non-protocol, non-class type 'Array<I>'
class B<I: Equatable, Context: Array<I>, List: Array<I>> {}
                               ^
main.swift:8:49: error: inheritance from non-protocol, non-class type 'Array<I>'
class B<I: Equatable, Context: Array<I>, List: Array<I>> {}
                                               ^
*/

The S<X: Y> syntax specifies a generic parameter X that inherits from or implements Y. Since you can neither inherit from, nor implement Array<T>, B won't compile.

A compiles because the generic argument is not constrained to anything, and the generic typealias just assigns a type to it.

If all you want is an Array of I, you don't need to include that in the generic arguments at all. You can use Array<I> freely in the type. If that's not descriptive enough, you could include a typealias.

If you do want it to be a generic parameter conforming to something else, Sequence for example, the syntax for protocols with associated types works like this:

class C<I: Equatable, Context: Sequence, List: Sequence> where Context.Element == I, List.Element == I {}

Thank you for the clarification. I actually need the base class with the extra parameters because the context and list types can change in subclasses.

It was just to clarify my thinking on the errors in case I came across the same thing again :smiley: