Hossam
(sam)
1
protocol RandomNumberGenerator {
func random() -> Double
}
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
}
}
//You can extend an existing type to adopt and conform to a new protocol
protocol TextRepresentable {
var textualDescription: String { get }
}
//The Dice class from above can be extended to adopt and conform to TextRepresentable:
extension Dice: TextRepresentable { // here is the issue, TextRepresentable //is ambiguous for type lookup in this context
var textualDescription: String {
return "A (sides)-sided dice"
}
}
let d12 = Dice(sides: 12, generator: LinearCongruentialGenerator())
print(d12.textualDescription)
//must Prints "A 12-sided dice"
Gero
(Gero Herkenrath)
2
Hello and welcome to the forums, @Hossam!
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).
Hossam
(sam)
3
Thanks for replying,
the issue is that (see this code below , is not running because TextRepresentable is ambiguous )
extension Dice: TextRepresentable { // here is the issue
var textualDescription: String {
return "A (sides)-sided dice"
}
}
sveinhal
(Svein Halvor Halvorsen)
4
If you put your code between ``` ticks like these:
```
extension Dice { ... }
```
It will look like this:
extension Dice { ... }
Gero
(Gero Herkenrath)
5
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.
1 Like
Hossam
(sam)
6
it running now, thank you very much ,
i don't know why was not running but after i copied you code it runs straight away.
Gero
(Gero Herkenrath)
7
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!
1 Like