[swift-users] Swift Initializer Generics

Dmitri Gribenko gribozavr at gmail.com
Mon Jan 18 18:54:06 CST 2016


On Mon, Jan 18, 2016 at 4:23 PM, Tyler Fleming Cloutier via
swift-users <swift-users at swift.org> wrote:
> Is there no way to get this done with generics? Writing the function with
> generic parameters yields the following error:
>
> func normalized<T where T: FloatingPointType, T: FloatLiteralConvertible>(x:
> [T]) -> [T] {
>     return x / sum(x) // Error: Cannot invoke 'sum' with an argument list of
> type '([T])'
> }

Fixing this essentially requires a redesign of Surge to be centered
around protocols, I think.  You would need to make sure that every
entry point that is implemented differently for Floats and Doubles is
called through a protocol requirement.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-users mailing list