Restricted parameterized class member

I am very new to Swift (like a few days new) and stumbled upon a problem I am sure has a simple solution. But my lack of fluency prevents me from finding it so hope that someone would grudgenly say: RTFM
Anyway, here is a basic extraction

protocol TProtocol {
  associatedtype T
  func doubleValue(value: T) -> T
}
class I: TProtocol {
  typealias T = Int
  func doubleValue(value: Int) -> Int {
    return value*2
  }
}
class S: TProtocol {
  typealias T = String
  func doubleValue(value: String) -> String {
    return value + value
  }
}
// I know ho to do it for a generic method
class C<T> {
  var value: T
  init(value: T) {
    self.value = value
  }
  func getValue<P: TProtocol>( proto: P) -> T where P.T == T {
    return proto.doubleValue(value: self.value)
  }
}
// DON'T KNOW HOW TO DO IT FOR THE MEMBER!!!
class B<T> {
  var value: T
  var proto: any ParamType // Should not be any but limited to the same T as the B itself
  init(value: T, proto:  B<T>.ParamType<T>) {. // this is not compiling
    self.value = value
    self.proto = proto
  }
//The idea is to return T without a cast
  func getValue<P: TProtocol>( proto: P) -> T { 
    return self.proto.doubleValue(value: self.value)
  }
}

I need a parametrized member to be restricted, I figured out how to do it for a generic method with where clause but can't figure out how to do the same restriction for a data member.

I could NOT explain what I need to ChatGPT, I am sure that people here have better skills :)

Thanks!

In the same way as you restricted method using where you can restrict a type:

class B<T> where T: TProtocol {}
1 Like

Thank you very much, I think you provided what I was looking for.