Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,220 --> 00:00:05,300
In the last section, we took a look at the minimum function right here, and we had said that we were
2
00:00:05,300 --> 00:00:07,730
going to start to talk about types inside of darte.
3
00:00:08,180 --> 00:00:13,040
So in this section, we're going to get a overview of the type system in Darte and we're going to kind
4
00:00:13,040 --> 00:00:14,500
of clarify a couple of things about it.
5
00:00:14,660 --> 00:00:15,770
So let's get to it.
6
00:00:16,800 --> 00:00:20,460
I've got a couple of rules right here, list on the screen, so these are a couple of items that we're
7
00:00:20,460 --> 00:00:21,170
going to go over.
8
00:00:21,210 --> 00:00:24,180
That's going to help clarify how types work inside of darte.
9
00:00:24,780 --> 00:00:29,400
Before we go over these items, I want to just mention very quickly that if you've never worked with
10
00:00:29,400 --> 00:00:34,230
a strongly typed language before, so if you come from a background like, say, JavaScript or Ruby
11
00:00:34,530 --> 00:00:41,160
and you feel like types are generally a little bit intimidating or confusing, I really recommend you
12
00:00:41,160 --> 00:00:42,600
not get too worried about it.
13
00:00:42,960 --> 00:00:47,760
Dart's type system is very approachable and relatively pretty straightforward to get used to.
14
00:00:48,210 --> 00:00:50,550
So with that in mind, let's start looking at these items.
15
00:00:51,570 --> 00:00:57,000
So every single value that we declare inside of a program has a type associated with it.
16
00:00:58,500 --> 00:01:03,210
Likewise, every variable that we declare has a type that it can reference.
17
00:01:03,630 --> 00:01:07,730
Now these two sides right here are already getting a little bit tricky and kind of interesting.
18
00:01:08,070 --> 00:01:11,880
So we just said every value has a type and every variable has a type.
19
00:01:12,420 --> 00:01:15,870
So what's the difference between a variable and a value?
20
00:01:16,290 --> 00:01:19,920
Well, I want you to consider one of the lines of code that we were just looking at.
21
00:01:19,920 --> 00:01:24,240
And we're going to break this down piece by piece and figure out what the difference between a value
22
00:01:24,270 --> 00:01:25,530
and a reference is.
23
00:01:26,250 --> 00:01:28,150
OK, so we're going to look at this line right here.
24
00:01:28,650 --> 00:01:33,900
Remember, we say var name, which declares our variable, and then on the right hand side we call the
25
00:01:33,930 --> 00:01:35,040
my name function.
26
00:01:35,820 --> 00:01:38,890
When we call my name we return the string, Stephen, right here.
27
00:01:39,240 --> 00:01:44,760
So essentially you and I can kind of imagine that this line of code right here kind of is equivalent
28
00:01:44,760 --> 00:01:45,720
to this.
29
00:01:45,720 --> 00:01:46,200
Right.
30
00:01:46,200 --> 00:01:48,910
Not not terribly different, at least somewhat similar.
31
00:01:49,200 --> 00:01:53,370
So let's just kind of make that assumption for just a second and take a look at a diagram that's can
32
00:01:53,370 --> 00:01:57,030
help us understand the difference between a value and a variable.
33
00:01:59,630 --> 00:02:05,140
So that whenever that line of code runs, this is what's really going on inside of your computer's memory.
34
00:02:06,170 --> 00:02:12,380
The code to the left hand side of the equals, where we do that variable declaration creates a new space
35
00:02:12,380 --> 00:02:16,220
inside of your computer's memory that holds that name variable.
36
00:02:16,940 --> 00:02:23,480
The name variable has the ability to reference another value that is stored inside of your computer's
37
00:02:23,480 --> 00:02:23,900
memory.
38
00:02:24,880 --> 00:02:29,440
Then the right hand side of the equals sign, so the my name over here where we return the string of
39
00:02:29,440 --> 00:02:33,760
Stephen creates what is known as a value inside of memory as well.
40
00:02:34,540 --> 00:02:40,540
So in that entire line of code executes, we end up with a variable or a reference over here on the
41
00:02:40,540 --> 00:02:44,770
left hand side and a value over here on the right hand side.
42
00:02:45,680 --> 00:02:51,720
The value has a type of string associated with it, so this over here, it is of type string.
43
00:02:52,120 --> 00:02:56,100
So that's why we say that every value has a type associated with it.
44
00:02:56,790 --> 00:03:02,940
But likewise, that thing on the left, the thing that is referencing that value can have a type associated
45
00:03:02,940 --> 00:03:03,740
with it as well.
46
00:03:04,570 --> 00:03:11,380
So because this name variable over here is pointing at a value with type string, that implicitly means
47
00:03:11,380 --> 00:03:15,610
that the name variable can only reference other values of type string.
48
00:03:16,450 --> 00:03:21,490
So what I mean by that is that after we have made that association right there between name and string
49
00:03:21,490 --> 00:03:27,730
right here, if I then created another value inside of my application, like, say, the integer, one,
50
00:03:27,730 --> 00:03:34,810
two, three, I could not have this name variable over here reference that integer because this name
51
00:03:34,810 --> 00:03:37,750
variable has already been associated with type string.
52
00:03:38,890 --> 00:03:44,020
Now, looking at this in diagram format and giving you a verbal description might be a little bit confusing.
53
00:03:44,020 --> 00:03:48,220
So let me give you a little bit more practical example back over inside of D'Arte Pat.
54
00:03:49,170 --> 00:03:51,390
When you and I just declared that variable right here.
55
00:03:52,370 --> 00:03:58,250
Of name and then assigned a string to it, that means that name can only reference a string.
56
00:03:58,760 --> 00:04:04,910
So if I then try to assign the value of, say, name equals one to three, like, so I'm going to end
57
00:04:04,910 --> 00:04:10,790
up seeing an error message that says, hey, you're trying to assign a value of type Int to a variable
58
00:04:10,790 --> 00:04:11,790
of type string.
59
00:04:12,500 --> 00:04:17,839
So again, when we start thinking about types inside of darte, there is a difference between values
60
00:04:17,839 --> 00:04:19,130
and references.
61
00:04:19,339 --> 00:04:22,220
And we always want to be aware of the differences between the two.
62
00:04:23,810 --> 00:04:28,410
OK, let's go continue down this list right here, so we just spoke about the third item right here.
63
00:04:28,580 --> 00:04:33,080
Once a variable has a type associated with it, as we're doing back on that line of code over in Dart
64
00:04:33,110 --> 00:04:37,990
Pad right now, we cannot change the type of value that that variable can reference.
65
00:04:38,510 --> 00:04:45,470
So once we say my name right here has string type associated with it, we cannot not say, oh, well,
66
00:04:45,470 --> 00:04:48,560
now I want name to instead store a integer instead.
67
00:04:51,590 --> 00:04:56,600
Now, the last drill that we're going to talk about is that this is where darts type system is very
68
00:04:56,600 --> 00:04:57,170
approachable.
69
00:04:57,170 --> 00:05:03,950
If you've never worked with static type before, Dart can often guess the type of a given variable for
70
00:05:03,950 --> 00:05:04,300
us.
71
00:05:04,490 --> 00:05:09,680
And so we do not always have to specifically annotate that type of a variable.
72
00:05:10,190 --> 00:05:12,160
Let me show you a very practical example of this.
73
00:05:12,620 --> 00:05:17,960
So back over inside of our code over here, I want you to click on the variable name up here at the
74
00:05:17,960 --> 00:05:18,950
top left hand side.
75
00:05:19,490 --> 00:05:25,610
When we do so, you'll notice at the bottom right panel down here says that the variable name is of
76
00:05:25,610 --> 00:05:26,660
type string.
77
00:05:27,630 --> 00:05:34,590
So Dart is clever enough to see that, ah, my name function right here returns a value of type string.
78
00:05:35,400 --> 00:05:42,000
And we take that value of type string and assign it to the name variable, so DARTE has enough information
79
00:05:42,000 --> 00:05:48,900
by looking at your program to infer or kind of guess that name is supposed to reference a value of type
80
00:05:48,900 --> 00:05:49,440
string.
81
00:05:51,290 --> 00:05:56,570
That's kind of the purpose of putting the string keyword down right here by annotating the return type
82
00:05:56,570 --> 00:06:01,910
of this function, Dart has a little bit more information about what's going on inside of our program
83
00:06:01,910 --> 00:06:05,780
and can do a better job of guessing the different types that are flowing around it.
84
00:06:06,530 --> 00:06:11,300
Now, one thing I want to mention is that we do not necessarily have to always annotate every single
85
00:06:11,300 --> 00:06:12,610
type inside of our program.
86
00:06:13,040 --> 00:06:19,220
So, for example, if we wanted to, we could have dropped off this entire string annotation right here.
87
00:06:20,050 --> 00:06:24,820
Now, you'll notice that I do not have any errors in my code like nothing is turning red, and if I
88
00:06:24,820 --> 00:06:27,850
click on run up here, my code still runs appropriately.
89
00:06:28,540 --> 00:06:34,510
However, Dart no longer has enough information to actually guess about the type of the variable name
90
00:06:34,510 --> 00:06:34,960
right here.
91
00:06:35,500 --> 00:06:39,670
And if you click on it, you'll notice that now the type is labeled as dynamic.
92
00:06:40,450 --> 00:06:45,100
When you see a type of dynamic, that means that Dart is essentially saying, you know what, I don't
93
00:06:45,100 --> 00:06:51,190
really know what type this variable is, I'm going to label it as a catch all type, which is the dynamic
94
00:06:51,190 --> 00:06:51,580
type.
95
00:06:52,270 --> 00:06:57,150
The dynamic type essentially means the dart just doesn't really know what type this variable is.
96
00:06:57,760 --> 00:07:03,460
And so only by having you and I, as developers annotate some specific functions or specific variables
97
00:07:03,460 --> 00:07:08,680
inside of our application, do we give DART just enough information to figure out exactly what's going
98
00:07:08,680 --> 00:07:08,980
on?
99
00:07:09,610 --> 00:07:14,770
So if I add string back on here as a type annotation to the my name function.
100
00:07:15,690 --> 00:07:22,140
And click on my name or named again up here, D'Arte again has enough information to figure out what
101
00:07:22,140 --> 00:07:23,340
type that variable should be.
102
00:07:24,570 --> 00:07:29,640
OK, so in this section, we spoke about a couple of confusing rules about types inside of dirt.
103
00:07:30,360 --> 00:07:35,750
First off, remember, every value has a type and every variable has a type that it can reference.
104
00:07:36,630 --> 00:07:41,010
Once a variable has a type associated with it, we cannot magically change that type at some point in
105
00:07:41,010 --> 00:07:41,570
the future.
106
00:07:42,120 --> 00:07:48,180
And then finally, you and I as developers can optionally annotate the types of functions or variables
107
00:07:48,360 --> 00:07:53,730
to give Dart a little bit more information about what value types are flowing around our application.
108
00:07:54,770 --> 00:07:59,310
At this point, this type stuff might still seem really confusing, but don't sweat it.
109
00:07:59,330 --> 00:08:03,250
We're going to be talking about types throughout this course just nonstop.
110
00:08:03,500 --> 00:08:05,480
So you'll get a lot of experience with types.
111
00:08:05,870 --> 00:08:10,490
And even at the end of the day, if you still feel like types are confusing after this course, the
112
00:08:10,490 --> 00:08:17,180
good news is that honestly, to some degree, you can kind of just ignore them in a lot of programs.
113
00:08:17,180 --> 00:08:22,610
You can kind of just drop them off entirely, have no type's labeled and still write a working program.
114
00:08:22,970 --> 00:08:26,450
Now, there's definitely downsides to that that we'll talk about quite a bit throughout the course.
115
00:08:26,690 --> 00:08:30,890
But all I'm trying to say is that if types seem very strange, don't sweat it.
116
00:08:30,890 --> 00:08:32,210
It's not the worst thing in the world.
117
00:08:32,809 --> 00:08:34,240
OK, so let's take a pause right here.
118
00:08:34,400 --> 00:08:36,049
We're going to come back in the next section.
12662
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.