Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Earlier we learned about type properties, but let's switch gears here and learn about type methods.

0:00
Value and reference semantics are a bit of a mind boggling topic.

0:03
So let's switch to something more straightforward for now.

0:07
We started this course by looking at a simple concept, type properties.

0:11
Just like we can add properties to the type itself, we can also add type methods.

0:16
But you knew this, because we have already written a type method in the past.

0:20
When we did that we use the keyword static, but we jumped ahead a bit.

0:24
So let's dial it back and work our way through some of the different options and

0:28
what they mean.

0:29
So we'll add a new comment and

0:31
we'll say type methods in here I'll say this is the Value Type section.

0:39
So in here let's create a struct again one that we've seen earlier the Map.

0:45
When we did this earlier Map also included a type or

0:48
a static property origin that contained a point.

0:51
So we'll say static let origin and to this we'll assign a point instance.

0:57
Given an arbitrary point,

0:59
we want to calculate the distance of that point from the origin.

1:03
We could make this an instance method but the computation doesn't depend

1:08
on any values being assigned to stored properties.

1:11
The only information we need given a point

1:13
is the location of the origin which is a static property.

1:17
And this could be a free function, but

1:19
since it deals with an aspect of the Map, we're going to add it to our type.

1:23
To create a type method, we're going to start with a keyword static.

1:27
After that, we write the method out just like we would any instance method.

1:32
So func distance(to point: Point) and this is going to return a double value.

1:42
This method takes a point and computes the distance from the origin.

1:46
And this involves a bit of math, so I'll just do the work for you.

1:50
First we calculate the horizontal distance,

1:52
that is the distance between the origin's X coordinate and the point's X coordinate.

1:57
So let horizontal distance equal origin.x,

2:01
and you'll see here that because this method,

2:05
the type method is scoped or is same level as the property.

2:10
They're both on the type we can access it here without having to say it Map

2:14
origin dot x.

2:15
Because both of these are at the Map level and not the instance level.

2:20
And this will say minus or subtract point.x.

2:25
Similarly we'll need the vertical distance and

2:29
we'll use the respective coordinates so origin.why point.y.

2:35
Now to get the distance using these values, again we'll do a bit of math.

2:39
And the way we do this is first we're going to square both of these

2:44
values then we're going to get this sum of the two and

2:46
return the square root of that resulting value.

2:49
To square the values we can simply multiply horizontal value by itself but

2:54
that kind of makes our code look ugly.

2:56
We could write a helper method square that takes a value and

2:59
returns the value multiplied by itself.

3:02
But if we try that and

3:03
add it as an instance method, you'll notice that you won't be able to use

3:07
it inside a static method because you first need to create an instance.

3:11
Now we could add it as a second type method but

3:14
unlike instance methods helper type methods, aren't really a thing.

3:18
You only want methods strictly associated with the types functionality,

3:22
swift however lets you do a cool thing and

3:24
that is nesting functions inside other functions.

3:28
So inside this method I can write a small helper function to use.

3:31
We'll say func square, it takes a value

3:37
and returns a Double value and in here we'll simply say return value * value.

3:43
And now we can use this function, because it's available inside this method, we'll

3:48
say let horizontalDistanceSquared

3:54
= square and pass in the horizontal distance value.

3:59
And then we'll do the same thing for the vertical distance.

4:02
I'm going to copy that, paste it, and

4:05
I'll just change this to vertical and this to vertical.

4:10
Okay, for the last bit of logic we need to return the square root

4:15
of the sum of these values because again math.

4:18
And there's a square root function defined in the foundation framework.

4:22
So right over here we'll say import Foundation and

4:27
then at the bottom we'll return the value of horizontal distance squared

4:31
into that we'll add the value of vertical distance squared.

4:35
Right, so first we'll get the sum of these two values and

4:38
then return the square root of it.

4:39
Notice in this example we're using a value type.

4:43
With value types we always declare both type methods and

4:47
type properties with the static keyword.

4:50
Static here refers to two things.

4:53
One, that this method is associated at the type level rather than an instance.

4:58
And second that the method is statically dispatched.

5:02
A simple way to think about static dispatch is that the compiler knows which

5:06
method you intend to call at compile time.

5:10
Now hold onto that definition for a second.

5:12
By comparing it to dynamic dispatch in the next video

5:15
we should be able to get a better understanding of both.
You need to sign up for Treehouse in order to download course files.
Sign up