Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In the last section, we took a look at the minimum function right here, and we had said that we were
going to start to talk about types inside of darte.
So in this section, we're going to get a overview of the type system in Darte and we're going to kind
of clarify a couple of things about it.
So let's get to it.
I've got a couple of rules right here, list on the screen, so these are a couple of items that we're
going to go over.
That's going to help clarify how types work inside of darte.
Before we go over these items, I want to just mention very quickly that if you've never worked with
a strongly typed language before, so if you come from a background like, say, JavaScript or Ruby
and you feel like types are generally a little bit intimidating or confusing, I really recommend you
not get too worried about it.
Dart's type system is very approachable and relatively pretty straightforward to get used to.
So with that in mind, let's start looking at these items.
So every single value that we declare inside of a program has a type associated with it.
Likewise, every variable that we declare has a type that it can reference.
Now these two sides right here are already getting a little bit tricky and kind of interesting.
So we just said every value has a type and every variable has a type.
So what's the difference between a variable and a value?
Well, I want you to consider one of the lines of code that we were just looking at.
And we're going to break this down piece by piece and figure out what the difference between a value
and a reference is.
OK, so we're going to look at this line right here.
Remember, we say var name, which declares our variable, and then on the right hand side we call the
my name function.
When we call my name we return the string, Stephen, right here.
So essentially you and I can kind of imagine that this line of code right here kind of is equivalent
to this.
Right.
Not not terribly different, at least somewhat similar.
So let's just kind of make that assumption for just a second and take a look at a diagram that's can
help us understand the difference between a value and a variable.
So that whenever that line of code runs, this is what's really going on inside of your computer's memory.
The code to the left hand side of the equals, where we do that variable declaration creates a new space
inside of your computer's memory that holds that name variable.
The name variable has the ability to reference another value that is stored inside of your computer's
memory.
Then the right hand side of the equals sign, so the my name over here where we return the string of
Stephen creates what is known as a value inside of memory as well.
So in that entire line of code executes, we end up with a variable or a reference over here on the
left hand side and a value over here on the right hand side.
The value has a type of string associated with it, so this over here, it is of type string.
So that's why we say that every value has a type associated with it.
But likewise, that thing on the left, the thing that is referencing that value can have a type associated
with it as well.
So because this name variable over here is pointing at a value with type string, that implicitly means
that the name variable can only reference other values of type string.
So what I mean by that is that after we have made that association right there between name and string
right here, if I then created another value inside of my application, like, say, the integer, one,
two, three, I could not have this name variable over here reference that integer because this name
variable has already been associated with type string.
Now, looking at this in diagram format and giving you a verbal description might be a little bit confusing.
So let me give you a little bit more practical example back over inside of D'Arte Pat.
When you and I just declared that variable right here.
Of name and then assigned a string to it, that means that name can only reference a string.
So if I then try to assign the value of, say, name equals one to three, like, so I'm going to end
up seeing an error message that says, hey, you're trying to assign a value of type Int to a variable
of type string.
So again, when we start thinking about types inside of darte, there is a difference between values
and references.
And we always want to be aware of the differences between the two.
OK, let's go continue down this list right here, so we just spoke about the third item right here.
Once a variable has a type associated with it, as we're doing back on that line of code over in Dart
Pad right now, we cannot change the type of value that that variable can reference.
So once we say my name right here has string type associated with it, we cannot not say, oh, well,
now I want name to instead store a integer instead.
Now, the last drill that we're going to talk about is that this is where darts type system is very
approachable.
If you've never worked with static type before, Dart can often guess the type of a given variable for
us.
And so we do not always have to specifically annotate that type of a variable.
Let me show you a very practical example of this.
So back over inside of our code over here, I want you to click on the variable name up here at the
top left hand side.
When we do so, you'll notice at the bottom right panel down here says that the variable name is of
type string.
So Dart is clever enough to see that, ah, my name function right here returns a value of type string.
And we take that value of type string and assign it to the name variable, so DARTE has enough information
by looking at your program to infer or kind of guess that name is supposed to reference a value of type
string.
That's kind of the purpose of putting the string keyword down right here by annotating the return type
of this function, Dart has a little bit more information about what's going on inside of our program
and can do a better job of guessing the different types that are flowing around it.
Now, one thing I want to mention is that we do not necessarily have to always annotate every single
type inside of our program.
So, for example, if we wanted to, we could have dropped off this entire string annotation right here.
Now, you'll notice that I do not have any errors in my code like nothing is turning red, and if I
click on run up here, my code still runs appropriately.
However, Dart no longer has enough information to actually guess about the type of the variable name
right here.
And if you click on it, you'll notice that now the type is labeled as dynamic.
When you see a type of dynamic, that means that Dart is essentially saying, you know what, I don't
really know what type this variable is, I'm going to label it as a catch all type, which is the dynamic
type.
The dynamic type essentially means the dart just doesn't really know what type this variable is.
And so only by having you and I, as developers annotate some specific functions or specific variables
inside of our application, do we give DART just enough information to figure out exactly what's going
on?
So if I add string back on here as a type annotation to the my name function.
And click on my name or named again up here, D'Arte again has enough information to figure out what
type that variable should be.
OK, so in this section, we spoke about a couple of confusing rules about types inside of dirt.
First off, remember, every value has a type and every variable has a type that it can reference.
Once a variable has a type associated with it, we cannot magically change that type at some point in
the future.
And then finally, you and I as developers can optionally annotate the types of functions or variables
to give Dart a little bit more information about what value types are flowing around our application.
At this point, this type stuff might still seem really confusing, but don't sweat it.
We're going to be talking about types throughout this course just nonstop.
So you'll get a lot of experience with types.
And even at the end of the day, if you still feel like types are confusing after this course, the
good news is that honestly, to some degree, you can kind of just ignore them in a lot of programs.
You can kind of just drop them off entirely, have no type's labeled and still write a working program.
Now, there's definitely downsides to that that we'll talk about quite a bit throughout the course.
But all I'm trying to say is that if types seem very strange, don't sweat it.
It's not the worst thing in the world.
OK, so let's take a pause right here.
We're going to come back in the next section.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.