It would probably be helpful if you edited and formatted your question so that the source code is easier to read. If you're unfamiliar with markdown there's a GUI for it right above the edit field, just put your source code snippets inside a "Preformatted text" block (the </> symbol).
You have to provide indents to make it more readable then, too.
That being said, I don't really know what you mean with your question. On first glance and a quick copy&paste to a playground above code seems fine. Conforming to a protocol via an extension like is totally fine.
Update: I just noticed that your one protocol is called RandomNumberGenerator, be aware that the standard library also contains a protocol with that name. That seems unrelated to your question, though? (If you have need for both you can still reference the standard library's one via Swift.RandomNumberGenerator, btw).
Please format your code as @sveinhal explains (which I should have done as well in the first place... ).
I understood that you meant to indicate you see a problem in that line, but whatever causes the warning/error in your codebase is not part of what you posted.
Allow me to post a commented, formatted, and slightly rearranged version of youd code:
// MARK: - The protocols
protocol RandomNumberGenerator {
func random() -> Double
}
protocol TextRepresentable {
var textualDescription: String { get }
}
// MARK: - The classes
class Dice {
let sides: Int
let generator: RandomNumberGenerator
init(sides: Int, generator: RandomNumberGenerator) {
self.sides = sides
self.generator = generator
}
func roll() -> Int {
return Int(generator.random() * Double(sides)) + 1
}
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy: m))
return lastRandom / m
}
}
// MARK: - Making Dice adopt TextRepresentable in an extension
extension Dice: TextRepresentable { // this totally compiles
var textualDescription: String {
return "A \(sides)-sided dice"
}
}
let d12 = Dice(sides: 12, generator: LinearCongruentialGenerator())
print(d12.textualDescription) // this prints as expected
I don't get what would cause a "TextRepresentable is ambiguous for type lookup in this context" warning/error, in fact I have not seen that one before myself.
You can check yourself by copying & pasting this into a fresh, empty playground (I am using Xcode 14.0.1, so Swift 5.7) and let it run, it works. I have no idea what causes your warning/error without more context.
Nice, glad it now works (though it would be interesting to know what caused this). For what it's worth: There was one typo in your original code that I simply omitted as it seemed unrelated: In your Dice's roll() method there were two ** after your return statement, but that just has the compiler error on that line and has nothing to do with TextRepresentable. I assumed that just came to be when posting your code as the post has, as stated, some weird/lacking formatting in the first place.
Have fun coding, and again, welcome to the forums!