How to update this code to use parameter packs?

I have this piece of code that used different arity for each number of generic type count, but I can't figure out a way to make it use variadic generics.

protocol P {
  init(source: A)
}

class A {
  ...
}

@dynamicMemberLookup
class C1<L1: P>: A {
  subscript(dynamicMember keyPath: KeyPath<L1, A>) -> A {
    return L1(source: self)[keyPath: keyPath]
  }
}

@dynamicMemberLookup
class C2<L1: P, L2: P>: A {
  subscript(dynamicMember keyPath: KeyPath<L1, A>) -> A {
    return L1(source: self)[keyPath: keyPath]
  }
  subscript(dynamicMember keyPath: KeyPath<L2, A>) -> A {
    return L2(source: self)[keyPath: keyPath]
  }
}

@dynamicMemberLookup
class C3<L1: P, L2: P, L3: P>: A {
  subscript(dynamicMember keyPath: KeyPath<L1, A>) -> A {
    return L1(source: self)[keyPath: keyPath]
  }
  subscript(dynamicMember keyPath: KeyPath<L2, A>) -> A {
    return L2(source: self)[keyPath: keyPath]
  }
  subscript(dynamicMember keyPath: KeyPath<L3, A>) -> A {
    return L3(source: self)[keyPath: keyPath]
  }
}

I have tried this. but i can't come up with a compiling subscript signature

@dynamicMemberLookup
class CN<each L: P>: A {
  subscript(dynamicMember keyPath: KeyPath<each L, A>) -> A { // <---❓
    return each L(source: self)[keyPath: keyPath] // <---❓
  }
}
1 Like

Currently variadic packs allows to create functions with several arguments of related types. In your case I think it is not supposed that subscript can take several key paths like subscript(dynamicMember keyPath1: KeyPath<L1, A>, keyPath2: KeyPath<L2, A>), keyPath3: ...

Instead several subscript overloads are needed, something like:

class CN<each L: P>: A {
  repeat
  subscript(dynamicMember keyPath: KeyPath<each L, A>) -> A {
    return each L(source: self)[keyPath: keyPath]
  }
}

Similar topic has also appeared in another discussion, here is @scanon answer:

I've created a thread, though it should be named better to generalize both cases:

1 Like