Why Scala? | An introduction by Adam Warski

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning my name is adam varsky i'm the cto and co-founder at software mill we are a software development and consulting company and today i would like to spend about 10 minutes probably a couple more to try to encourage you to check out scala and try to show some of the productivity benefits that you get when you are using scala so why scala but before we dive into why scholar what is color scala is a strongly typed functional programming language it runs predominantly on the jvm but also it compiles down to javascript and native byte code and i think it is currently the best way to do functional programming in a business in a business setting so scala has all of the features all of the basic features of any program of any functional programming language so we have lambdas we have functions which can be treated as values we have methods which can be converted to functions which are again values and the whole language is expression oriented everything is an expression and if is an expression uh switches an expression and so on so this is the basis of really of any functional programming language out there uh we have data classes we have pattern matching so all of the basics are covered but what makes scala unique are some of the more advanced features some of the features that are not there in other languages and so for example we've got term inference you might be familiar with type and friends so type in friends is when the compiler is smart enough to figure out the type of an expression that we have written term inference works the other way around it's when the compiler is smart enough to figure out the expression bending basing on the type that we have given it so these are scalars implicits we also have higher order types which make it possible to create really elegant a concise and expressive dsls to manipulate your your data scala is also a very regular language it doesn't really have that many features but all of the features it has are very general and and they interact in in many ways some of these combinations of features are really interesting some of them are not that interesting but i think as a community we have learned which combinations to avoid and which to use finally scala has a quite rich meta programming capabilities meta programming is when you write code which is run at compile time it generates more scalar code which is then translated into bytecode javascript and so on but let's look at some some specifics so first of all let's look at data modeling and data modeling is something that we have to do very often and it's one of the areas where scholarly shines if you have to model a complex business domain scala is a very expressive language and gives you a lot of tools to do that so here we have a case class a case class is kind of a data class all of the instances of a case class will be immutable and so once we create an instance we cannot change any data here and so we have a couple of fields a one of them is optional so in scala we don't really use nulls and null pointer exceptions and nulls are not really an issue most of the time and that is because everybody uses options all of the libraries use options to express uh well optional fields instead of instead of using nulls so this is assembly this is a simple data class you can also define a data classes which form a hierarchy so here you have an interface a trade entity which has two implementations that interface is sealed meaning that all of the implementations that are possible are enumerated in this source file so it's either a person or a company and so this means that if you have an instance of an entity you can be sure that it's either person or a company and the compiler will verify that for you here we also have a collection and a list in this case of addresses um all of the collections that you are going to be working with by default in scala all of them are immutable so all of the lists sets maps and so on all of these collections are immutable immutable by default as well this brings us to the first productivity gain which is that scala is an immutable first language and this has deep implications when it comes to to the ecosystem to the libraries all of the libraries that are written for scotland there's quite a lot of them a work predominantly with immutable data structures with immutable collections and so on so this they form a cohesive whole um and they they really leverage the fact that immutability that immutability is the preferred way of writing scala and this makes things easier to understand and it also makes things partially thread safe so you have less problems to worry about um scala is also very expressive when it comes to manipulating data so let's say we have a set of streets so again this set is immutable let's say we have a set of entities and we want to extract all of these streets corresponding to these entities so we take those entities and what we do is we extract uh we extract first the addresses as a first stage so as i said there are two possibilities an entity can be either person a company so if it's a person we take the addresses convert the addresses to a set if it's a company we create a single element set which is the headquarters of it company that way we obtain a set of addresses and then we can map this to to the streets and get a set of streets so this way of manipulating collections and manipulating data is something that's very common in scala you're going you are going to do you are going to work with values like this a lot and a lot of concepts are represented as values in scala in general as you will see later as well um these kind of operations here where we have been working with an in-memory collection but they carry over to a more complicated structures as well so for example if you are manipulating a big data set in spark the code will look quite similar this brings us to the second productivity gain of scala which is uh that the language uh allows you to create a concise uh but still re is readable which i think is very important and typesafe code so type save here the compiler will actually verify that uh we can access the properties uh that that we've written and that for example here that we have covered all of the cases and and i think the code is concise uh it's not really much you can remove from here and and even if you don't know a lot of scala you will probably more or less understand what is happening in that method but let's let's carry on so let's look at some meta programming which i was talking about in the beginning so quite often you know in in any programming language we have to do http requests and when doing http requests well we have to serialize data to json and visualize it from json and so in other languages libraries and so on quite often the approach that is taken is um introspecting data classes at runtime for example using reflection and basing on that doing the serialization and decentralization logic here the approach is different what we are going to do is we are going to use a method which is in fact a macro a macro is something that runs at compile time generate scalar code which is then translated further into byte code so this this macro over here it is parameterized with a type so the type is the the input for that for that method and it will generate code which knows how to serialize an instance of an address into json so once we have an instance of an encoder which is really a function from address to json once we have an instance of that we can be sure that we know how to serialize that class into json similarly with parsing uh the response and all of this happens at compile time and this brings us to the third project productivity gain which i wanted to stress over here is that in scala we often get compile time proofs instead of runtime tests so this color compiler really verifies a lot of properties or for us at compile time it's true that sometimes it does take some time to actually compile the code but it takes this time for a reason and that is because well it actually does quite a lot of work checking that our code is is correct when it comes when it comes to the types and quite often instead of running a simple unit or integration tests multiple times we can just rely on the compiler to verify a lot of these basic properties for us and verify on the more and um focus on the more complex ones when running the tests so this brings us to the next example so here we have an example of how you can send an http request using scala so what we do is we first describe the http request that we want to send as a value as i said we are working with values in scala a lot so here we are representing the http request to be sent as a value you can see that there is the address uh to which the request should be sent and there's the body which to include with the request and also a specification of what to do with the response so you might note here that as a body we are setting an instance of an address so what the library really needs to know is how to convert this instance of an address into a serialized http body right so what it does what it is it needs an encoder for this for this type and actually the compiler will figure out this reference here by itself and this code will not compile if there isn't in if an encoder for an address is not present um in the current scope and so here we have an example of the term inference that i was mentioning in the beginning uh so here the body method actually requires or looks up an instance given a type so it needs an encoder for an address type and it looks up an instance an expression that satisfies the types same for the response handling over here so again if this code compiles we can once again be sure that we can actually send this request and parse the response so once we have our description of a request ready which is just a value we can actually send it using the send function which you know does all the network stuff that is needed and this brings us to another productivity gain of scala that in scala we are mostly using libraries instead of frameworks so frameworks are very often constraining while libraries give us a lot of flexibility and freedom so we don't have to find the framework anymore we can just choose the libraries that work best for our use case still it's worth noting that you know libraries have to form cohesive packages or they have to work well with each other so you know you have to pick libraries they also like from their own small ecosystems but still you get a lot more freedom than uh comparing to you to using a framework the two libraries that we've been using in this example are http client for http requests and cersei for handling json so finally going to the last productivity game that i would like to mention and scala really emphasizes types and the fact that it's a strongly typed language is one of its main features it has an advanced type system the goal of any type system is to make illegal states unrepresentable and i think scholar really pushes the boundary as to how how many states or how much you can push how how much you can verify using the type system while still being a practical language of course you there are still some states that will be illegal but are representable but it's a much smaller fraction comparing to other languages and you know computers are really intelligent enough to prove a lot of things about our code so that we don't have to test it by hand and so that we don't have to fix it by running and repeatedly running these uh tests so we can just delegate a lot of this work to the computer and that's what the scala compiler does finally i think that tests are really a crucial component not test but types are really a crucial component when it comes to the explorability and the way we can understand and exist a new code base that's well a code base that is new for us so tools like autocomplete really help to discover what's available on a data type or what kind of possibilities an api gives us and we also have find usages and go to definition uh which are great tools when exploring a new library or when exploring a new a new code base so these are simple tools i think available in any ide but they really make a difference and the the fact that the language is typed here also makes it easy to to actually make these kinds of functional functionalities possible it's worth mentioning that is color 3 is coming up the next major version of the language it polishes a lot of the rough edges of scala too all of most well most of the features of scala to remain some of them are improved and some new features are added so take a look at scala 3 if you are interested probably uh by the end of this year early next year we should have a release candidate uh of of of the language and and that's all that i had thank you very much if you would like to start discovering scala i can recommend ours our scholar starting page scala dot page you will find a lot quite a lot of materials on how to set up your local computer how to which books to consider some podcasts videos articles tutorials and so on if you're interested in scala in general you can subscribe to scholar times it's a weekly version of scala news and if you have any questions my dms on twitter are open so feel free to reach out to me and with that thank you very much
Info
Channel: SoftwareMill
Views: 25,456
Rating: 4.8895464 out of 5
Keywords: why, what, what is scala, scala tutorials, scala tutorial for beginners, Overview, Scala, Scala programming, functional programming, Introduction to Scala, Scala Programming Tutorial, What is Scala Programming, Scala Tutorial, Scala language, Scala introduction
Id: t7SOXNJVbJo
Channel Id: undefined
Length: 15min 58sec (958 seconds)
Published: Tue Oct 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.