Avatar ·

How to extend the functionality of typed arrays (Array<T>) in Swift?

Hi everyone! I've been writing in Swift for a few months now and encountered a situation where I need to add custom methods to arrays of a specific type. For example, I have an array of numbers `[Int]` and I want to add a method to calculate the sum of squares of all elements. Or an array of strings `[String]` to which I want to add a method to remove all duplicates. I tried writing an extension for Array, but it applies to all arrays, not just Int or String: ```swift extension Array { func sumOfSquares() -> Int? { // Here I can't guarantee that Element is Int return nil } } ``` The compiler complains that you can't perform the `*` operation on Element. I tried using where constraints but didn't fully understand the syntax. Please advise how to properly extend A