What output will be produced by the code below?

func square<T>(_ value: T) -> T {
    return value * value


Correct answer: This code will not compile.

Explanation: This code has attempted to create a generic number squaring function, but has failed to declare that T(the data type being used) has the ability to work with the * operator, so Swift cannot compile it. One solution might be to modify it to square<T: Integer>, which would allow it to work on IntUIntInt64 and so on.



© 2017