[swift-users] Protocol Conformance

Muhammad Tahir Vali tahir.vali13 at gmail.com
Mon Jun 19 18:38:00 CDT 2017

Hey all,

I wanted to know if theres a work around for a problem I am having

lets say I have a protocol

protocol *Graphable* : CustomStringConvertible, Sequence, Collection {
    var vertices : [AnyVertexable] { get set }
*    var edges: [AnyEdge]? { get set }*

Then I have 2 classes that inherit from them

class *EZUndirectedGraph* : Graphable {
    var vertices : [AnyVertexable]
*    var edges: [AnyEdge]? *

class *EZDirectedGraph* : Graphable {
    var vertices : [AnyVertexable]
*    var edges: [AnyEdge]? *
*    // *var edges : [AnyEdge]

Is there a way for me to make the "edges" variable in "*EZDirectedGraph*"
to NOT be an optional while still conforming to the same protocol? As one
may know, a condition for directed graphs requires there to be atleast 1

Advice or alternative workarounds would be nice with respect of what I am
trying to do here with Graph Theory.

My last 2 options were to either

1. Create separate protocols for the different types of graphs but there's
many types so I would be writing a lot of redundant code.

2. Leaving it how it is. Inserting an enum "TypeOfGraph" for the different
types of graphs and then computing its type with the help of the different
initializer methods.

Best Regards,

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

More information about the swift-users mailing list