You do not want the generic constraint if you happen to go the kind as a variable to compute
like this:
struct Laptop {
static func compute(with anyP: P.Sort) -> P {
anyP.occasion()
}
}
Alternatively, if the ensuing kind of compute
shouldn’t be P
explicitly, you can put the generic constraint into the tactic:
struct Laptop {
static func compute<T: P>(with anyP: T.Sort) -> T {
anyP.occasion()
}
}
After which name compute
like this:
for worth in someArray {
let occasion = Laptop.compute(with: worth)
occasion.doSomething()
}
This assumes that P
has the tactic doSomething
. If you wish to entry a way that’s particular to A
, you’ll at all times have to kind forged. Ultimately occasion
will at all times be a P
to the compiler, as a result of it has to imagine that the identical code can be executed for any values inside someArray
, which is P.Sort
. Irrespective of whether or not compute
returns P
or explicitly T
, occasion
will nonetheless be inferred to be P
if you happen to place it in a loop like this.