As showing in the image, i'm already using types such as MilkChocolate which conforms to the "Chocolate" protocal, and im trying to declare a function which returns a "some chocolate", which means return a type of chocolate depends on the input, but it gives me an error says
Function declares an opaque return type, but the return statements in its body do not have matching underlying types
this confused me by a lot and i think MiklChocolate is an underlying type for the Chocolate protocol
whats wrong?
and my code should match this tutorial: https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html
PS: if you cant view the image, here's the code
protocol Chocolate {
associatedtype Content
var mass:Double {get}
}
struct Honey {}
struct Milk {}
struct Wine {}
class HoneyChocolate: Chocolate {
typealias Content = Honey
var mass = 1.0
}
class MilkChocolate: Chocolate {
typealias Content = Milk
var mass = 1.2
var mysteriousEffect = true
}
class WineChocolate: Chocolate {
typealias Content = Wine
var mass:Double = 999
}
func giveMeChocolate(of type:String) -> some Chocolate {
switch type {
case "honey":
return HoneyChocolate()
case "milk":
return MilkChocolate()
default:
return WineChocolate()
}
}