Last active
March 17, 2017 00:43
-
-
Save jamesrochabrun/ce4d7c8aa016ca4a1e4f5e8599887b5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//helper struct | |
struct Point { | |
var x: Double | |
var y: Double | |
} | |
//type methods - valueType section | |
//you cant override in value types because they don't have inheritance | |
struct Map { | |
//type property | |
static let origin = Point(x: 0, y: 0) | |
static func distance(to point:Point) -> Double { | |
let horizontalDistance = origin.x - point.x | |
let verticalDistance = origin.y - point.y | |
//nested functions are a good way because we can't use a helper method inside a static function | |
func square(_ value:Double) -> Double { | |
return value * value | |
} | |
let horizontalDistanceSquared = square(horizontalDistance) | |
let verticalDistanceSquared = square(verticalDistance) | |
//foundation methid | |
return sqrt(horizontalDistanceSquared + verticalDistanceSquared) | |
} | |
} | |
//with value types we always declare both type methods and type properties with the static keyword.(if we want them to be in the type level) | |
//static here referes to two things: | |
//1 - that this method is associated at the type level rather than an instance | |
//2 - the method is statically dispatched. this means that the compiler knows which method you intend to call at compile time | |
//type methods - reference type section | |
class Calculator { | |
class func squareRoot(_ value:Double) -> Double { | |
return sqrt(value) | |
} | |
//what if we don't want to override a method? | |
//we can use the keyword final or static | |
final class func addTen(to value:Double) -> Double { | |
return value + 10.0 | |
} | |
} | |
Calculator.squareRoot(81) | |
//final keyword if you dont want your class be subclassed | |
final class NewCalculator: Calculator { | |
//we can override the method of super class | |
override class func squareRoot(_ value:Double) -> Double { | |
return sqrt(value + 10) | |
} | |
} | |
//when you annotate a method with the class keyword, not only does it create a type method, but it allows you to override the method in subclasses to provide your own implementation | |
//methods in reference types are dinamycally dispatched and means that when you call in this case the squareRoot method, the compiler does not know which specific method we are calling | |
// it at run time figures out where the implementaion is, which class it's defined in and then calls in that specific class. | |
//static or final applies to both type methods in an instance marking it as final | |
//IN GENERAL ALWAYS USE STATIC TO CREATE TYPE METHODS, THE ONLY TIME YOU SHOULD CREATE A DYNAMICALLY DISPATCHED TYPE METHOD (ONE THAT CAN BE OVERRIDEN) IS IF YOUR SUBCLASSES REALLY NEED TO OVERRIDE IT | |
//THE FINAL KEYWORD CAN ALSO BE APPLIED TO INSTANCE AND CLASSES | |
//What keyword do we use to create a type method for value types? | |
//static | |
//What keyword do we use to create a type method for reference types? | |
//class | |
//static can also be used to prevent the method be overriden | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment