Live data from Hacker News

Swift: When Unused Code Is a Bug

peripheryapp.com

1–10 of 77 posts

Re: Swift: When Unused Code Is a Bug

#3
Default implementations on protocols in Swift definitely can be a little dangerous. We've shied our team away from doing this unless the protocol is explicitly used as a mix-in type.

There's been some proposals around fixing these, one that comes to mind is: https://forums.swift.org/t/introducing-role-keywords-to-redu...

Re: Swift: When Unused Code Is a Bug

#4

I wonder if this behavior is working as intended. It does look like a bug to the language itself, isn't?

There are some rules around this, as noted in https://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future:

  IF the inferred type of a variable is the protocol:
    AND the method is defined in the original protocol
      THEN the runtime type’s implementation is called, irrespective of whether there is a default implementation in the extension.
    AND the method is not defined in the original protocol,
      THEN the default implementation is called.
  ELSE IF the inferred type of the variable is the type
    THEN the type’s implementation is called.

Re: Swift: When Unused Code Is a Bug

#5

I wonder if this behavior is working as intended. It does look like a bug to the language itself, isn't?

It appears the correct action would have been to use override func greet() { print("sup") }.

To be fair, I was primed to really think about the code execution given he'd indicated there was a trap in there somewhere, so it certainly would not have been immediately obvious on first glance. But I think, generally developers understand that override is necessary to override code from a parent class.

Typically XCode helps out with this sort of thing – adding override where it seems like it should be added – but perhaps not when a parent class is using a default protocol implementation.

But again, in this scenario where a class is inheriting from another class, it's typically known you need to override the function in order to use your own implementation, so this does seem a bit contrived.

Re: Swift: When Unused Code Is a Bug

#6
I've run afoul of this a couple of times in my Swift delegate protocols. A really nice hack to implement Objective-C's optional protocol methods in Swift is to use a protocol extension to create a default method that can come back to bite you when you subclass objects that conform to that protocol.

Re: Swift: When Unused Code Is a Bug

#8

I wonder if this behavior is working as intended. It does look like a bug to the language itself, isn't?

It appears the correct action would have been to use override func greet() { print("sup") } . To be fair, I was primed to really think about the code execution given he'd indicated there was a trap in there somewhere, so it certainly would not have been immediately obvious on first glance. But I think, generally developers understand that override is necessary to override code from a parent class. Typically XCode hel…

> It appears the correct action would have been to use override func greet() { print("sup") }.

That actually doesn't work because the greet function is implemented on an extension. You can only override class functions.

Re: Swift: When Unused Code Is a Bug

#9
If I saw this in a code review, I would flag it for re-design. Mixing composition and inheritance especially in this way is definitely going to confuse anyone who doesn't read every line to figure out what's going on.
Post reply on HN