I have this code, and I am trying to make some methods unavailable under certain conditions. In Swift, I have seen methods appear grayed out with a red exclamation mark, indicating that they cannot be used in a specific instance (or maybe that that thing is going to be deprecated in the future). This might have been due to some experimental code, and learning swift.
Is there a way to make the unwrap() method from GeneralArithmetic “grayed out” or unavailable for certain structures?
Here is the code:
enum UnwrapError: Error {
case notApplicable(String)
}
protocol GeneralArithmetic {
func unwrap() throws -> GeneralArithmetic
}
protocol Arithmetic: GeneralArithmetic {
func evaluate() -> Int
}
protocol ArithmeticBoolean: GeneralArithmetic {
func evaluate () -> Bool
}
struct const: Arithmetic, CustomStringConvertible {
let value: Int
init(_ value: Int) {
self.value = value
}
func evaluate() -> Int {
return value
}
func unwrap() throws -> GeneralArithmetic {
throw UnwrapError.notApplicable("Available: evaluate()")
}
var description: String {
return "const((value))"
}
}
struct negate: Arithmetic, CustomStringConvertible {
let exp: Arithmetic
init(_ exp: Arithmetic) {
self.exp = exp
}
func evaluate() -> Int {
return -exp.evaluate()
}
func unwrap() throws -> GeneralArithmetic {
return exp
}
var description: String {
return "negate((exp))"
}
}
struct add: Arithmetic, CustomStringConvertible {
<#...#>
}
struct multiply: Arithmetic, CustomStringConvertible {
<#...#>
}
struct bool: ArithmeticBoolean, CustomStringConvertible {
<#...#>
}
// Error: UnwrapError.notApplicable("Available: evaluate()")
let result = try const(5).unwrap()
// const(5)
//let result2 = try negate(const(5)).unwrap()
So, it will look like this:
try const(5). -> (maybe you can see the method unwrap, but is grayed out)
try negate(const(5)).unwrap() -> (now is working fine)
Is there a way to achieve this in Swift, or should I approach it differently? Any suggestions are appreciated!