WEBVTT

00:00.000 --> 00:09.000
Chapter 15 is about how to
use Scala string interpolators

00:09.560 --> 00:15.039
So a string interpolator just lets you put
variables and expressions inside of a string

00:15.640 --> 00:19.520
So what I do in the book is I
start with these two variables

00:20.120 --> 00:26.559
I'll paste those into the REPL. Then I note that you could
print the name like that, the full name like that, but

00:26.559 --> 00:31.159
this is the better and much, much, much
more preferred way to do these things

00:31.239 --> 00:36.040
What I'm doing here is I embed, I have the
two variable names, first name and last

00:36.040 --> 00:41.720
name, and then I embed those in the string. And the way this
works is you start with the character S before the string, and

00:41.720 --> 00:47.639
that's the Scala, let's just call it the basic string
interpolator. And that allows us to put variable names in there

00:47.639 --> 00:53.080
And we proceed those variable names with the dollar sign as shown
here. So it's dollar sign, first name, dollar sign, last name

00:53.159 --> 01:00.360
And then that will print Alvin Alexander. And if you
wanted to, you could also include those inside of these

01:00.360 --> 01:07.559
curly braces like this, though. It's not needed when
you do a single variable there, but those do become

01:07.559 --> 01:12.040
needed when you put an expression
inside there. So I do have those here

01:12.040 --> 01:15.720
So I will just copy and paste
those rather than type from scratch

01:16.599 --> 01:21.239
So here I'm putting an expression 2
plus 2 inside of these curly braces

01:21.360 --> 01:28.599
So you see that result there. This is going to fail
on multiple levels if I take those out, because

01:28.599 --> 01:34.919
dollar sign 2 doesn't mean anything. And then my
intention was to add 2 plus 2 there, and that won't work

01:34.919 --> 01:40.599
you know, without these curly braces. So again, anytime
you want to have a, anything, any sort of expression

01:41.040 --> 01:46.519
inside of a string interpolator, use the curly
braces with the dollar sign as shown right there

01:47.199 --> 01:55.199
And one other thing I need to mention also is I keep showing examples with
print line, but it's important to know that the string interpolators can't

01:55.199 --> 01:59.720
the result of a string interpolator
can be assigned to a variable like this

01:59.720 --> 02:04.959
So I say, val X equal that. Let me clear up
that screen for just a moment and do that again

02:05.239 --> 02:12.520
So here I say, val X equals this interpolated string. So
then X becomes a string and it has that result right there

02:12.520 --> 02:16.119
So again, the key here is you
don't, this can be used anytime

02:16.119 --> 02:20.399
not just with print line. So I use it
here to assign the result to a string

02:21.320 --> 02:28.880
And then the next thing I want to show here is that there are other interpolators.
So as I mentioned in the book, it seems a little awkward, like when you first

02:28.880 --> 02:34.440
start using this, it seems a little awkward that you have to put the letter
S in there before an interpolator because other languages don't do that

02:34.440 --> 02:38.520
But then once you start seeing these other
interpolators, you see the wisdom of the approach

02:38.839 --> 02:45.279
So the basic interpolator is that S one there. And then I
mentioned like when you start using third party libraries, like SQL

02:45.279 --> 02:49.440
libraries, a lot of times they will put
the words, the characters SQL before a

02:49.440 --> 02:54.839
string and that uses their SQL interpolator. So what
happens is you can write your own interpolators

02:55.080 --> 02:59.800
So SQL is one. And then the one I really
want to mention here is this one is built in

03:00.080 --> 03:10.320
and it's the F interpolator and it's used for formatting. So I show
an example in the book here about how this format string, excuse me

03:10.839 --> 03:17.679
How this format string leads to this output. And I don't want
to get into this in too much detail because frankly, I can't

03:17.679 --> 03:24.000
remember it all, but if I go back through
here let me, so I'll create a variable X

03:24.880 --> 03:29.399
And assign it to the value 10. And then I
want to use that with the F interpolator

03:30.679 --> 03:36.160
And so I can do, let me start with this
one here and let me do that result

03:36.160 --> 03:41.440
So you can see the result as well. So what's going on
here is I start with the F interpolator right there

03:41.800 --> 03:47.360
And then I'm creating a string with double quotes. And
then I say, I want to use the variable dollar sign X

03:47.919 --> 03:57.600
I'm sorry. I want to use the variable X. So I have to proceed that with a dollar
sign. And then I want to use this formatting symbol right there, the percent 5D and

03:57.600 --> 04:08.279
percent 5D is kind of like a standard print F format. So if you
come over to this excellent page here and just replace devdaily.net

04:08.279 --> 04:14.199
with alvinalexander.com, then you will see examples
here controlling integer with, with print F

04:14.199 --> 04:19.359
So you have examples like this, like percent
3D, percent 3D with different numbers here

04:20.640 --> 04:24.359
And so that's what I'm doing here
is I'm saying, I want to print an

04:24.359 --> 04:30.640
integer, but give it five spaces. So it does that and
a better, not a better, but another way to demonstrate

04:30.640 --> 04:36.359
that is I go back here and now I say,
instead of percent 5D, I say percent 05D

04:37.359 --> 04:45.000
And that zero fills that 10 area. So here I had three
spaces, one zero, and then this one zero fills it

04:45.000 --> 04:51.679
So I have three zeros followed by the one zero. This
works again, because I'm using the F interpolator that

04:51.679 --> 04:56.559
will not work with the S. And just to demonstrate
that, you know, that's totally different

04:56.559 --> 04:59.959
result, not what you want. So
whenever you want to format output
