Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Created May 23, 2016 13:35
Show Gist options
  • Select an option

  • Save oleksii-demedetskyi/db226193b1a32af74f2073294f3a3f1e to your computer and use it in GitHub Desktop.

Select an option

Save oleksii-demedetskyi/db226193b1a32af74f2073294f3a3f1e to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Cocoa
// Operations
protocol Stringable {
func stringify() -> String
}
protocol Evaluatable {
func evaluate() -> Double
}
// Types
protocol Expr {}
struct Constant: Expr {
let value: Double
}
struct BinaryPlus<LH: Expr, RH: Expr>: Expr {
let lh: LH, rh: RH
}
// Add implementations
extension Constant: Evaluatable {
func evaluate() -> Double {
return value
}
}
extension Constant: Stringable {
func stringify() -> String {
return String(value)
}
}
extension BinaryPlus: Stringable where LH: Stringable, RH: Stringable {
func stringify() -> String {
return "\(lh.stringify()) + \(rh.stringify())"
}
}
extension BinaryPlus: Evaluatable where LH: Evaluatable, RH: Evaluatable {
func evaluate() -> Double {
return lh.evaluate() + rh.evaluate()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment