Protocols Are Confusing Me

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Protocols Are Confusing Me

Thomas Wetmore
I have a protocol issue that is confusing me. With these protocols:

protocol Event
{  func dateq () -> Date? }

protocol Date
{   func yearq () -> Int? }

and these classes:

class GedcomEvent: Event
{
    init () { ... }
    func dateq() -> GedcomDate?  // Error: Protocol requires function 'dateq()' with type '() -> Date?'
    { ... }
}

class GedcomDate: Date
{
init () { ... }
    func yearq () -> Int? { return nil }
}

I get the compiler error shown above. If I replace 'GedcomDate?' in the offending line with 'Date?' the compiler is happy. 

Is this expected behavior? It doesn't seem right to me. Yes, a GedcomDate must meet the Date protocol, but within the specific GedcomEvent class I also want to use specific properties of the GedcomDate class that are not reflected in the Date protocol. Isn't it the case with the real dateq() method having to return a Date? instead of a GedcomDate? that I am loosing the details of the full GedcomDate class?

Tom Wetmore

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/4a6bc2c0-938c-4118-bcc6-8acc068dc7f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Protocols Are Confusing Me

Derrick Hathaway

Is this expected behavior? It doesn't seem right to me. Yes, a GedcomDate must meet the Date protocol, but within the specific GedcomEvent class I also want to use specific properties of the GedcomDate class that are not reflected in the Date protocol. Isn't it the case with the real dateq() method having to return a Date? instead of a GedcomDate? that I am loosing the details of the full GedcomDate class?

Hi Tom,
What you’re looking for is called covariant return types. Most object-oriented languages have support for this including Objective-C. Swift doesn’t yet have support. I would bet that this will be supported eventually. The previous post is somewhat related and links to an interesting article about that types of type variance that Swift currently supports:


In the short term you will have to downcast Date to GedcomDate in cases where you have enough type info to do so:

let event = GedcomEvent()

    // sometime later
    if let gedcomDate = event.dateq() as? GedcomDate {
        // do something with gedcomDate which now has the type GedcomDate
    }

hopefully covariant return types will be supported eventually.

Cheers,
Derrick

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2591A0A0-C6E9-48B8-B247-D2514AEE0A66%40instructure.com.
For more options, visit https://groups.google.com/d/optout.