Snippet from Swift Language Guide doesn't compile

The first snippet in Protocol Extensions chapter of Swift Language Guide doesn't compile.

import Foundation

extension RandomNumberGenerator {
    func randomBool() -> Bool {
        return random() > 0.5 // Binary operator '>' cannot be applied to operands of type 'Int' and 'Double'
    }
}

Should I bug report this?

Yeah, go ahead and file on Feedback Assistant and it'll get to the right place.

I think the specific issue is that this chapter has an example protocol that predates the standard library's RandomNumberGenerator protocol, so there's a name conflict. Earlier in the chapter, it defines:

protocol RandomNumberGenerator {
    func random() -> Double
}
3 Likes

Yeah the documentation has grown a bit stale, especially due to the standard library gaining RNG support.

See also: https://bugs.swift.org/browse/SR-10063

Terms of Service

Privacy Policy

Cookie Policy