#1 - Dart Language, Type System, Soundness, Type Inference, Null Safety, JIT & AOT Compilers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i'm wicked and welcome back to my dart from novice to expert complete course today i want to give you a brief introduction on dart so that you'll have somehow a better understanding on the particularities of dart language i will make sure that every short topic i'll address in this video will have its separate more in-depth tutorial later on in this course therefore today i just wanted to make an overall idea on the entire dart echo system without further ado let's get right into it so what is dart i'm sure most of you never ask yourself this simple question but i find it really interesting to know the story of dart as something tangible or perceivable before diving into all its particularities therefore if you take a look at the logo of the art at first glance you might say that well it's just a weirdly designed shape but as soon as you look closely you can notice the shape is actually the rear end of a dart arrow think about this for a second a player can throw this dart arrow at a dartboard during a dart game it might seem a little bit funny but in the previous analogy lies the entire definition of the dark language or so i think it does we think of the dart arrow as being the dart language the dark player as being the dark developer and the dart board as being the entire ecosystem of dart based apps now think about the particularities of a dart arrow first and foremost the dart arrow has to be really really precise that is to be as optimized as possible in order for the player to throw it precisely at the dartboard secondly it also better be fast the average speed of a dart hitting a board is around 64 kilometers per hour which is a lot for such a small object this means that the language not only has to be designed in a minimalist way easy to understand for everyone but it also needs to have really fast compilation targets on web mobile and desktop platforms it also has to be tough even though the dart arrow is more sized when thrown at the board the last thing a player wants is for it to completely shatter into pieces this translates into the language being stable enough to maintain scalability maintainability and readability over time not only that but the player can retrieve the arrow back and throw it again and again and again as many times it's needed the dart has to behave the same way each and every time thus the dart language amazing feature of hot reload and as always there is no such thing as playing darts without having a stable game platform built on the principles of a dart dart language form the foundation of flutter in this case which is a really popular framework widely used around the world so having all these particularities in mind if we sum them up we'll notice that these are the main features of the dart language each and every one of them is included in dart's technical envelope which contains the choices made during development that shape the capabilities and strength of a language that is in our case the dart language now it's time for us to talk about some of the particularities of dart language let's start with the beginning dart is a type safe language in short this means that the only operations that can be performed on data in the language are those allowed by the type of the data if your data is of type x and type x doesn't support operation f then the language won't allow you to execute f of x it's that simple so for example if you have a variable x of type integer you can't perform operations sanctioned by the string type like to uppercase or to lowercase since the integer type doesn't support those at the same time if you have a variable y of type string you can't assign it to a value of type integer since the string type does not allow the assignment operation to an integer i hope it makes sense now you may wonder well then dart must have a tool analyzing whether the developer is writing typesafe code or not right and indeed it has dart uses a combination of static type checking at compile time held by the dart static analyzer and also run time checks in order to determine if the code is type safe or not it's really important to understand these two types of checks since we'll use this terminology a lot in the following minutes these type checks will make sure that if there's an error somewhere in the code either compilation fails or runtime exception is thrown this is sometimes referred to as a sound type system in short dart won't allow the code to run into undefined states but why exactly is it called sound well i'd like to imagine that whenever you type code on your keyboard you're making noise right imagine that for every key you press the sound print is being sent to the dart static analyzer he checks everything and makes sure that the code you wrote is type safe when he detects that something isn't right it will display an error and won't let you run the program at all until the issue is solved so a static type check is more like a local check preventing the developer from writing wrong types a runtime check is more like a double check a further check over the static type check that occurs when the program runs there are specific moments when you'd want the dart analyzer to stop listening to the code you're writing we'll talk more in depth about these moments in future tutorials but the thing is that dart allows you as a developer to tell the analyzer well mate i really know what i'm doing here you don't have to track me for each type i write to do this you'll need to use the dynamic type whenever you use the dynamic type the static analyzer won't care about that data anymore you can name a variable b of type dynamic assign it to something call perhaps a completely random method on it and the analyzer won't say a single word however after all if there is no static check available somebody still has to do the checkups and the checkups still need to be 100 legit right indeed these fire checkups are done at run time and if something goes wrong at runtime dart will throw an exception and if you ain't checking for exceptions in your code then the program will crash so you see dynamic type can become kind of dangerous but as i said there are moments where you really need this functionality and we'll talk about them later in this course also if you notice from the exception dart automatically knew the type you assigned the variable to even though we initially assign it to dynamic how is it possible we'll find out by studying the next topic called type inference so right until now we established that types are absolutely mandatory inside dart being a typesafe language but even though types are always mandatory you don't have to specifically annotate them each and every time that's because dart can also infer types you can let darth statically infer the type of fields methods local variables and more generic type arguments by using the var keyword the moment you assign something to it the dart analyzer automatically converts it to the type of the value it was assigned to and for the rest of its life it can only be of that specific type from this we can deduce that by using var keyword this conversion is happening using the static dart analyzer and not at runtime however as we said there are some cases in which you would want the check to be done at runtime rather than statically at compile time if you don't want the static dart analyzer to be in your way you can mark the variable as being of a dynamic type you can let dart dynamically infer the type of fields methods local variables and more generic type arguments by using the dynamic as we said before in this case dart checks types at runtime so only at runtime it will decide what the real type of the data is as an example assigning a type of dynamic to x won't let dart right away know that the type is double even though you know for sure that it is indeed a double dart will only find that at runtime when you run the program that means this x variable can be set to multiple types of data inside the code since at runtime dart will automatically convert it to the right type every time it's needed for beginners dynamic and var may create some confusion therefore to better see the difference between them take a look at these two programs first and foremost you need to understand an important aspect var isn't a type is just a keyword for telling the static dart analyzer hey i want you to statically assign the type for this specific data dynamic on the other hand is a standalone type on its own in the first case you'll see that there are no analyzer errors that is because for the variable named a the static analyzer is completely deactivated by annotating its type to dynamic the checkup and type inference is done at runtime as you can see during the runtime workflow it is completely normal for the variable a to have multiple types one time it's an integer then it's a double then it's a string this is what dynamic means in the end having the ability of some data to have multiple types but only at runtime in comparison taking a look at the second program using the var keyword forces the dar static analyzer to set the type of variable b to integer from that moment during its lifetime b variable can only be of type integer as you can see it can't be set to other types of data the analyzer won't allow that and will throw an error but now you may wonder what happens when you write this line of code what happens when the dart analyzer is forced to set a type to a variable and doesn't have enough information about the type it should set well analyzer will play it safe and set it to dynamic therefore passing the job to the runtime checker and if the runtime checker still doesn't know what type it should have it will set it to null by default however setting a variable to null is not a good practice and dart recently introduced the most expected feature of all time the sound null safety null safety means that values can't be null unless you say they can be sound null safety means dart can do these checks through its static code analysis via the trusty analyzer while also combining it as we said before with the runtime checks that means during its lifecycle inside dart data can either be nullable or non-nullable but never both unlike many other null saved languages when data determines that a variable is non-nullable that variable will always be non-nullable and that means the non-nullability is retained at runtime null safety is a really really important topic and i'll make a separate tutorial for it later in this course now that we understand in big words how dart uses these static and runtime checks it's time to move on to how dart turns your code into working programs so let's say you wrote a dart program how does dart transform this code into a functional program onto all of its supported platforms this is a really important question so make sure you pay as much attention as possible as any other language dart uses a compiler which is a tool that converts the source code you wrote in dart language into other intermediate languages or machine code that can run on a specific platform in a dart virtual machine however dart uses different compilers for different specific jobs for example on the cpu architecture listed on the screen right now usually found on desktops and phones dart uses a just in time compiler and an ahead of time compiler whereas on the web obviously it should use javascript compiler since it needs to translate the code into javascript language in order to understand what all these different types of compilers are designed for you need to understand that when you're writing code there might be two or more stages during your development process but always at least two one should be the development phase and the other one should be the production phase as developers during the development phase we want our code to be flexible easy to test and debug therefore during this phase the running application should benefit from multiple features like rich debugging support live matrix collections and perhaps fast coding cycles to iterate faster between code changes now imagine that you need a specialized compiler that lets you have all these features in the case of dart in order to run an application in development phase the recommended way of doing it is by having the source code compiled with jit which stands for just in time compiler just in time compiler as its name is implying compiles just the amount of code it needs just when it needs it so let's say if your code has 10 000 lines of code jit would only combine the amount of lines it needs for the moment and not the entire code this also means that whenever you run the app again it will use the already existing code compilation if nothing changes with that code jit also comes with incremental recompilation so that it will only recompile the modified part when needed jit compiler is the main star that enables hot reload in dart during development perhaps you heard about this amazing feature in flutter framework when you can modify a widget property when the app is running and it will automatically hot reload the changes into the app itself however you may see that jit compiler may come with a lot of features during the development stage but you might imagine that it's not the best nor the fastest and most optimal way to expose a program into the production phase first of all jt compiler doesn't transform source code written in dart into specific machine code language but rather into an intermediary language that can be run by the dart virtual machine this ensures that changes in development happen at a faster rate and without sacrificing a lot of time i.e you won't have to recompile the entire code every time you modify an integer value for example secondly i guess you can understand by yourself that while into production an app doesn't need to have nor the hot reload live metrics or rich debugging support the only thing that matters for the ordinary user is for the program or app to run with really fast startup times and to behave fast overall with no lag or whatsoever the only way an app or program can run really fast is by compiling its source code into native machine code on their specific platforms therefore dart should also benefit from a compiler that aids towards this and indeed it does it's called the aot compiler where aot stands for ahead of time compared to the just in time compiler the ahead of time compiler compiles the entire source code into the machine code supported natively by the platform it does this ahead of time before the platform runs the program that means when you decided to promote your code from development state to production state you need to use this compiler to do its specialized job and to benefit from the best and most optimized version of your code this has a drawback though of compiling the same code from scratch over and over again and thus it's not the best solution for testing or developing an app into the development stage in terms of web apps dart also has its own development and production compilers this time though they'll convert the dart code into javascript code to be able to run it on the web again we'll talk about all these compile methods more in depth in future tutorials so that you'll have a more practical approach to them anyways this was a tutorial for today i hope you really have a brief idea on what dart language is what are its most important particularities and how it can run code on multiple platforms in the next video we'll take an in-depth look at how to install dart sdk on windows mac os and linux and we'll also take a brief look over dart command line interface if you like this tutorial don't forget to hit the like button subscribe to my channel and share the video with all your colleagues and friends in search of top tier development until next time take care wiki is out bye bye
Info
Channel: Flutterly
Views: 4,489
Rating: undefined out of 5
Keywords: dart, dart lang, dart language, dartpad, dart.dev, dart overview, dart type system, sound type system, soundness, dart type inference, type inference, sound null safety, dart null safety, dart compilers, jit compiler, just in time compiler, aot compiler, ahead of time compiler
Id: nQRW0_Q9RFI
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.