10

I just started to learn Swift (v. 2.x) because I'm curious how the new features play out, especially the protocols with Self-requirements.

The following example is going to compile just fine, but causes arbitrary runtime effects to happen:

// The protocol with Self requirement
protocol Narcissistic {
    func getFriend() -> Self
}


// Base class that adopts the protocol
class Mario : Narcissistic  {
    func getFriend() -> Self {
        print("Mario.getFriend()")
        return self;
    }
}


// Intermediate class that eliminates the
// Self requirement by specifying an explicit type
// (Why does the compiler allow this?)
class SuperMario : Mario {
    override func getFriend() -> SuperMario {
        print("SuperMario.getFriend()")
        return SuperMario();
    }
}

// Most specific class that defines a field whose
// (polymorphic) access will cause the world to explode
class FireFlowerMario : SuperMario {
    let fireballCount = 42
    
    func throwFireballs() {
        print("Throwing " + String(fireballCount) + " fireballs!")
    }
}


// Global generic function restricted to the protocol
func queryFriend<T : Narcissistic>(narcissistic: T) -> T {
    return narcissistic.getFriend()
}


// Sample client code

// Instantiate the most specific class
let m = FireFlowerMario()

// The call to the generic function is verified to return
// the same type that went in -- 'FireFlowerMario' in this case.
// But in reality, the method returns a 'SuperMario' and the
// call to 'throwFireballs' will cause arbitrary
// things to happen at runtime.
queryFriend(m).throwFireballs()

You can see the example in action on the IBM Swift Sandbox here. In my browser, the output is as follows:

SuperMario.getFriend()
Throwing 32 fireballs!

(instead of 42! Or rather, 'instead of a runtime exception', as this method is not even defined on the object it is called on.)

Is this a proof that Swift is currently not type-safe?

EDIT #1:

Unpredictable behavior like this has to be unacceptable. The true question is, what exact meaning the keyword Self (capital first letter) has. I couldn't find anything online, but there are at least these two possibilities:

  • Self is simply a syntactic shortcut for the full class name it appears in, and it could be substituted with the latter without any change in meaning. But then, it cannot have the same meaning as when it appears inside a protocol definition.

  • Self is a sort of generic/associated type (in both protocols and classes) that gets re-instantiated in deriving/adopting classes. If that is the case, the compiler should have refused the override of getFriend in SuperMario.

Maybe the true definition is neither of those. Would be great if someone with more experience with the language could shed some light on the topic.

2
  • 1
    Even weirder: in a 7.3 Playground, it crashes with a general protection fault (!) just after printing "SuperMario.getFriend()".
    – Eric Aya
    Feb 8, 2016 at 12:56
  • BTW, I reported this as a bug after our discussion.
    – DarkDust
    Feb 16, 2016 at 17:01

2 Answers 2

6

Yes, there seems to be a contradiction. The Self keyword, when used as a return type, apparently means 'self as an instance of Self'. For example, given this protocol

protocol ReturnsReceived {

    /// Returns other.
    func doReturn(other: Self) -> Self
}

we can't implement it as follows

class Return: ReturnsReceived {

    func doReturn(other: Return) -> Self {
        return other    // Error
    }
}

because we get a compiler error ("Cannot convert return expression of type 'Return' to return type 'Self'"), which disappears if we violate doReturn()'s contract and return self instead of other. And we can't write

class Return: ReturnsReceived {

    func doReturn(other: Return) -> Return {    // Error
        return other
    }
}

because this is only allowed in a final class, even if Swift supports covariant return types. (The following actually compiles.)

final class Return: ReturnsReceived {

    func doReturn(other: Return) -> Return {
        return other
    }
}

On the other hand, as you pointed out, a subclass of Return can 'override' the Self requirement and merrily honor the contract of ReturnsReceived, as if Self were a simple placeholder for the conforming class' name.

class SubReturn: Return {

    override func doReturn(other: Return) -> SubReturn {
        // Of course this crashes if other is not a
        // SubReturn instance, but let's ignore this
        // problem for now.
        return other as! SubReturn
    }
}

I could be wrong, but I think that:

  • if Self as a return type really means 'self as an instance of Self', the compiler should not accept this kind of Self requirement overriding, because it makes it possible to return instances which are not self; otherwise,

  • if Self as a return type must be simply a placeholder with no further implications, then in our example the compiler should already allow overriding the Self requirement in the Return class.

That said, and here any choice about the precise semantics of Self is not bound to change things, your code illustrates one of those cases where the compiler can easily be fooled, and the best it can do is generate code to defer checks to run-time. In this case, the checks that should be delegated to the runtime have to do with casting, and in my opinion one interesting aspect revealed by your examples is that at a particular spot Swift seems not to delegate anything, hence the inevitable crash is more dramatic than it ought to be.

Swift is able to check casts at run-time. Let's consider the following code.

let sm = SuperMario()
let ffm = sm as! FireFlowerMario
ffm.throwFireballs()

Here we create a SuperMario and downcast it to FireFlowerMario. These two classes are not unrelated, and we are assuring the compiler (as!) that we know what we are doing, so the compiler leaves it as it is and compiles the second and third lines without a hitch. However, the program fails at run-time, complaining that it

Could not cast value of type
'SomeModule.SuperMario' (0x...) to
'SomeModule.FireFlowerMario' (0x...).

when trying the cast in the second line. This is not wrong or surprising behaviour. Java, for example, would do exactly the same: compile the code, and fail at run-time with a ClassCastException. The important thing is that the application reliably crashes at run-time.

Your code is a more elaborate way to fool the compiler, but it boils down to the same problem: there is a SuperMario instead of a FireFlowerMario. The difference is that in your case we don't get a gentle "could not cast" message but, in a real Xcode project, an abrupt and terrific error when calling throwFireballs().

In the same situation, Java fails (at run-time) with the same error we saw above (a ClassCastException), which means it attempts a cast (to FireFlowerMario) before calling throwFireballs() on the object returned by queryFriend(). The presence of an explicit checkcast instruction in the bytecode easily confirms this.

Swift on the contrary, as far as I can see at the moment, does not try any cast before the call (no casting routine is called in the compiled code), so a horrible, uncaught error is the only possible outcome. If, instead, your code produced a run-time "could not cast" error message, or something as gracious as that, I would be completely satisfied with the behaviour of the language.

1
  • Thank for the elaboration. I think that Swift should document how Self is to be interpreted, but I can't find it in the official specs. You are right that some things can be safely detected at runtime, but since this is a static type safety issue, I would be a lot happier if already the compiler catches it!
    – domin
    Feb 16, 2016 at 6:46
4

The issue here seems to be a violation in contract:

You define getFriend() to return an instance of receiver (Self). The problem here is that SuperMario does not return self but it returns a new instance of type SuperMario.

Now, when FireFlowerMario inherits that method the contract says that the method should return a FireFlowerMario but instead, the inherited method returns a SuperMario! This instance is then treated as if it were a FireFlowerMario, specifically: Swift tries to access the instance variable fireballCount which does not exist on SuperMario and you get garbage instead.

You can fix it like this:

class SuperMario : Mario {
    required override init() {
        super.init()
    }

    override func getFriend() -> SuperMario {
        print("SuperMario.getFriend()")

        // Dynamically create new instance of the same type as the receiver.
        let myClass = self.dynamicType
        return myClass.init()
    }
}

Why does the compiler allow it? It has a hard time catching something like this, I guess. For SuperMario, the contract is still valid: the method getFriend does return an instance of the same class. The contract breaks when you create the subclass FireFlowerMario: should the compiler notice that a superclass might violate the contract? This would be an expensive check and probably more suited for a static analyzer, IMHO (Also, what happens if the compiler doesn't have access to SuperMario's source? What happens if that class is from a library?)

So it's actually SuperMario's duty to ensure that the contract is still valid when subclassing.

6
  • What happens here is exactly what you observe. But the million dollar question is: Why does the compiler allow this situation to happen? Is it simply a bug? IMO, SuperMario should not be allowed to override getFriend the way it does.
    – domin
    Feb 8, 2016 at 13:20
  • Why shouldn't the compiler allow it? When SuperMario is defined, the restriction (getFriend must return an instance of SuperMario) is satisfied.
    – DarkDust
    Feb 8, 2016 at 13:21
  • I am not so sure about your formulation of this restriction. 'getFriend must return an instance of SuperMario' it not the same as 'getFriend must return an instance of Self, where Self has to be the type of self (lowercase), even in classes where this method gets inherited to'.
    – domin
    Feb 8, 2016 at 13:30
  • My feeling is that it is not that much work for the compiler. The return type can only be Self in this class and any further subclass, if it returns self directly, or something of type Self in this or any superclass.
    – domin
    Feb 8, 2016 at 13:55
  • 2
    I think you're trying to make the definition of Self broader/"smarter" than it is. You are right in that there is a loophole in the Self-requirement (and you found it), but again: why should the compiler prevent return SuperMario()? It is correct at that point… it only breaks when you create a subclass of SuperMario. If you feel this should/*can* be improved, I suggest you file a bug report. In the best case, they'll fix it; in the worst-case you'll get an authoritative answer why the behavior is the way it is.
    – DarkDust
    Feb 8, 2016 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.