Saturday, 15 February 2014

generics - Extend all types in Swift? -



generics - Extend all types in Swift? -

browsing swift's library code i've found:

extension t! : printable { var description: string { } }

the snippet seems extend types 'description' field. when seek same thing in code, error:

example.swift:10:11: non-nominal type 't!' cannot extended

protocol myprotocol { // ... } extension t! : myprotocol { // error: non-nominal... // ... }

there similar questions at:

how can extend typed arrays in swift? what's difference between optional<t> , optional types in swift? (error handling)

but fail address:

what's going on here? why library code okay, code... not? is possible types or types conform specific protocol?

first of all, not extend types. extends t!, implicitlyunwrappedoptional<t>. extends implicitly unwrapped optional type, not types.

the "header" not swift code; automatically generated. there may bugs in automatic header generator makes not generate true swift code in cases. don't take literally.

one weird thing notice in automatically-generated "header" built-in syntax contractions inconsistently applied -- type declaration doesn't utilize contraction, extension does:

struct array<t> extension t[] enum optional<t> extension t? struct implicitlyunwrappedoptional<t> extension t!

probably code in automatic header generator greedily replaces above types contracted syntax. first pretend extension t! says extension implicitlyunwrappedoptional<t>.

however, extension implicitlyunwrappedoptional<t> doesn't compile either, error "use of undeclared type 't'". in fact, in automatically-generated "header", wee many instances of extension declarations type parameters, e.g. extension dictionary<keytype, valuetype>, not compile in swift. bug automatic header generation.

but removing type parameter works:

extension implicitlyunwrappedoptional : myprotocol { // ... }

this shown in this answer.

generics swift

No comments:

Post a Comment