[swift-users] Error: Type 'Self.T' does not conform to protocol 'Equatable'

Muhammad Tahir Vali tahir.vali13 at gmail.com
Mon Jul 10 13:31:25 CDT 2017


hey all,

just trying to figuring out a few workarounds for a problem I'm having. Im
having 2 errors both because of associatedtype.

*#1 can be found on the "Vertexable" protocol. This is a strange error
which should work but I'm guessing associatedtypes are causing trouble.
  *
*//error: Type 'Self.T' does not conform to protocol Equatable*

*#2 is me trying to create an array of "Vertexable" objects that can have
different data types since there are use-cases of graphs with different
data types. I can do exactly this when I remove the associatedtype "T" but
I would need to remove  EZNode<T> as well and be forced to manually add it
in for the "EZVertex<Element: Equatable>" class.  *

Error is shown at bottom of this message. You can also refer to
https://www.natashatherobot.com/swift-type-erasure/
<https://www.natashatherobot.com/swift-type-erasure/>
for reference and try to create an array of "Pokemon"

Thank you in advance. Sorry if this was too bulky. Im just looking for
conceptual answers to workaround this or why compiler can't accept this.
My last option is to plainly remove both protocols but I find it quite
ironic if I would have to considering the philosophy of protocols.

*//Protocol to create a node*

*protocol** Nodeable : *Equatable*  {*

    *associatedtype** T*

    *var** data : T { **get* *set** }*

*}*


*//Value-based node*

*public* *struct** EZNode<Element: *Equatable*> : **Nodeable** {*

    *.....*

*    .....*


*    typealias** T = **Element*



    *var** data : **T*



    *init**(data: **T**) {*

        *self**.**data** = data*

*    }*



*}*

*//Protocol to create a vertex*

*protocol** Vertexable : Hashable {*

*    .....*

*    .....*

    *associatedtype** T*


    *var** node: *EZNode*<T> { **get* *set** }*

    *//error: Type 'Self.T' does not conform to protocol Equatable*


    *func** getData() -> T*



*}*


*//Reference-based vertex*

*class** EZVertex<Element : *Equatable*> : **Vertexable* *where* *Element**:
*Equatable* & *Hashable* {*



     *.....*

*    .....*

    *typealias** T = **Element*

    *var** node: **EZNode**<**T**>*

    *//alternative that requires manual entry since it doesnt belong to
"Vertexable" - var node: EZNod <Element>*



    *var** hashValue: *Int* {*

        *return* *"**\**(**self**.**index**) **\**(**self**.**node**)"**.*
hashValue

*    }*



    *init**(element: **Element**) {*

        *self**.**node** = **EZNode**(data: element)*

        *self**.**index** = index*

*    }*



    *static* *func** ==(lhs: **EZVertex**<**Element**>, rhs: **EZVertex**<*
*Element**>) -> *Bool* {*

        *return** lhs.**node**.**data* ==* rhs.**node**.**data* &&* lhs.*
*index** == rhs.**index*

*    }*



    *func** getData() -> **Element** {*

        *return* *node**.**data*

*    }*



*}*


*let** node1 = **EZNode**(data: **1**)*

*let** node2 = **EZNode**(data: **10.0**)*

*let** vertex1 = **EZVertex**(element: **node1**.**data**, index: **1**)*

*let** vertex2 = **EZVertex**(element: **node2**.**data**, index: **2**)*


 *//protocol "Vertexable" can only be used a generic constraint because it
has Self or associatedtype requirements. *

*let** arr1 : [**Vertexable**] = [**vertex1**,**vertex2**]*


-- 
Best Regards,

Muhammad T. Vali
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170710/a49fd9f5/attachment.html>


More information about the swift-users mailing list