KeyPath for generics

Hello friends , this is my first time writing on the Swift Forums. I was following this from a long time.

First time I am working on KeyPath. I have to set the values of the generic properties using KeyPath. Created a protocol which has the set method to set the new values.

   protocol TypedKeyPath {
  
    func set<V>(of entity: Entity, to newValue: V)
  }

extension ReferenceWritableKeyPath : TypedKeyPath {
    func set<V>(of entity: Entity, to newValue: V) {
            entity[keyPath: self] = newValue // error: Use of unresolved identifier 'entity'
    }
}

In my top class of my source are of type TypedKeyPath, I will set the value of my source using this method.

 func setValue(of entity: Entity)  {
       for (keypath, source) in sourceMap {
          let value = source.getValue()
          keypath?.set(of: entity, to: value)
    }

Problem is I have a compiler error. I am unable to solve this .

Have you meant to write something like this?

protocol TypedKeyPath {
    associatedtype R
    associatedtype V
    func set(of entity: R, to newValue: V)
}

extension ReferenceWritableKeyPath: TypedKeyPath {
    func set(of entity: Root, to newValue: Value) {
        entity[keyPath: self] = newValue
    }
}

This function cannot be generic, because if you have a keypath from A to B, then you can only use it with these two types

No, this will not work in my project. Its not possible to use generic on my sources.
Is there any other way to have a protocol/struct which will work as KeyPath for my sources.

What is “Entity”?

Entity is my object for whose property I am setting the value (example: Entity is School, College and they both have different properties)