Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,440 --> 00:00:05,670
In the last section, we spoke about type systems and darte, we're now going to move on to the very
2
00:00:05,670 --> 00:00:09,540
last line of code inside of our program, the print statement right here.
3
00:00:10,610 --> 00:00:14,570
The print function is a built in function that is included for us by default with.
4
00:00:15,470 --> 00:00:19,660
We use this print function to log out any particular value inside of our application.
5
00:00:20,150 --> 00:00:22,520
And so in this case, we are printing out the string.
6
00:00:22,520 --> 00:00:23,480
My name is.
7
00:00:23,690 --> 00:00:25,940
And then this dollar sign name thing right here.
8
00:00:26,900 --> 00:00:31,730
I think overall, the print statement is easy enough and it functions very similar to how print statements
9
00:00:31,730 --> 00:00:33,240
in other languages work as well.
10
00:00:33,680 --> 00:00:38,630
So let's turn our attention just a little bit to the string right here where we put in a dollar sign
11
00:00:38,630 --> 00:00:39,140
name.
12
00:00:39,980 --> 00:00:43,610
This is an example of string interpolation inside of darte.
13
00:00:44,390 --> 00:00:50,060
Whenever Dart sees a dollar sign like this inside of a string, it's going to try to find a variable
14
00:00:50,330 --> 00:00:54,290
defined with the same name as whatever comes directly after that dollar sign.
15
00:00:55,350 --> 00:00:59,340
If Dart is able to find a variable with the same name, it's then going to take the value from that
16
00:00:59,340 --> 00:01:01,770
variable and inject it into the string.
17
00:01:03,450 --> 00:01:08,580
Now, it's just a little bit more complicated than that in some cases when we want to use string interpolation.
18
00:01:08,850 --> 00:01:13,080
So let's talk about one case in which this syntax right here, it doesn't quite work out so well.
19
00:01:13,870 --> 00:01:19,570
As we mentioned just a moment ago in the last section, the string type inside of darte has a length
20
00:01:19,590 --> 00:01:24,520
property associated with it, the returns, the number of characters or that particular string.
21
00:01:25,240 --> 00:01:30,010
So let's try printing out the number of characters inside the name variable right here rather than just
22
00:01:30,010 --> 00:01:30,880
the string itself.
23
00:01:31,500 --> 00:01:35,380
So you might think that we could do a name at length like so.
24
00:01:36,520 --> 00:01:42,910
But if I run this program, you'll notice that I get an output of my name is Steven Length, so definitely
25
00:01:42,910 --> 00:01:44,260
not quite what we're expecting.
26
00:01:45,140 --> 00:01:50,390
The syntax that you're seeing over here for dollar sign and then the variable is only good when we are
27
00:01:50,390 --> 00:01:56,870
printing up some very simple value out of a variable so we can only use dollar sign and the variable
28
00:01:56,870 --> 00:02:02,180
name if we're just referencing the value of that variable and doing nothing else to it.
29
00:02:02,810 --> 00:02:08,180
But if we want to have any type of expression associated with this string interpolation like, say,
30
00:02:08,180 --> 00:02:13,820
looking up at property on a string, then we have to add in a set of curly braces around the entire
31
00:02:13,820 --> 00:02:14,480
expression.
32
00:02:15,140 --> 00:02:21,050
So to get this code right here working, I would have to do a dollar sign, curly brace and then wrap
33
00:02:21,050 --> 00:02:24,530
the entire expression with a set of curly braces like so.
34
00:02:25,640 --> 00:02:31,220
So now if I run this code, I'll see my name is and then seven, which is the number of characters inside
35
00:02:31,220 --> 00:02:31,890
the name variable.
36
00:02:32,510 --> 00:02:37,430
So again, the rule is that if we're printing out just a variable by itself, we can do a dollar sign
37
00:02:37,430 --> 00:02:38,420
and then the variable name.
38
00:02:38,750 --> 00:02:43,430
But if we're going to have any type of expression inside of here, we have to use the curly braces.
39
00:02:44,350 --> 00:02:48,550
Now, just one last thing I want to mention, if you always want to be completely safe and make sure
40
00:02:48,550 --> 00:02:53,050
that this string interpellation always works the way you expect, you can just always choose to use
41
00:02:53,050 --> 00:02:56,750
curly braces like so even if we're just referring to a simple variable.
42
00:02:57,310 --> 00:03:00,260
So if I run this now, I go back to my name is Steven.
43
00:03:00,850 --> 00:03:05,470
So again, if you want to be 100 percent safe all the time, you can just do dollar sign curly braces.
44
00:03:05,680 --> 00:03:10,240
But if you want to be just a little bit fancy, you can certainly omit the curly braces when it's just
45
00:03:10,240 --> 00:03:10,660
a variable.
46
00:03:11,590 --> 00:03:14,010
OK, so that's pretty much it for this code snippet right here.
47
00:03:14,380 --> 00:03:18,070
Let's continue in the next section and we'll continue on our next topic.
5076
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.