Skip to content

Instantly share code, notes, and snippets.

@philsquared
Created April 29, 2021 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philsquared/86763cd8639c85cc117622ad2f0a2a5f to your computer and use it in GitHub Desktop.
Save philsquared/86763cd8639c85cc117622ad2f0a2a5f to your computer and use it in GitHub Desktop.
protocol Square {
var size : Float{ get }
}
protocol Circle {
var radius : Float{ get }
}
class SquarePeg : Square {
let _size : Float
init( size : Float ) { _size = size }
var size : Float { get{ return _size } }
}
class RoundHole {
func fit( _ circleShapedThing : Circle ) {
print( "fit, with a radius of: \(circleShapedThing.radius)")
}
}
// ----------
extension SquarePeg : Circle {
var radius : Float{ get { return (_size*size*2).squareRoot() } }
}
let squarePeg = SquarePeg( size: 42.0 )
let hole = RoundHole()
hole.fit( squarePeg )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment