I'd like to achieve the following but can't fix the error: "Non-nominal type 'Element' does not support explicit initialization"
Original attempt:
public extension Array where Element: FloatingPointMathType {
func mean<Element>() -> Element {
let sum: Element = reduce (0.0, +) as! Element
return sum / Element(count) // ==> Non-nominal type 'Element' does not support explicit initialization
}
}
Also, I wonder why it requires the as! Element
cast
As a comparaison, a local function as follows compiles with no issue:
func mean<Element: FloatingPointMathType>(_ e: [Element]) -> Element {
let sum: Element = e.reduce (0.0, +)
return sum / Element(e.count)
}