Devreal

Scale By The Bay 2021 : Seth Tisue, Making the Transition to Scala 3

Scale By The Bay 2021 : Seth Tisue, Making the Transition to Scala 3

Recording: Scale By The Bay 2021 : Seth Tisue, Making the Transition to Scala 3

i'll turn it over to you now take it away awesome thank you uh and yes let's let's talk about straw three i think uh you know this this is traditionally uh a scholar conference so most people probably already know about scalab but you know just to remind uh uh just to remind you quickly it's a language which is both object oriented and functional we believe that these paradigms are complementary and not opposed it targets the jvm primarily but also javascript and native code via scholar native the language itself is built around a java like core so if you already know java you can begin with scala by writing java like code and then gradually incorporate more and more scala features scala is statically typed prioritizes type safety and it encourages you to work with immutable values that's part of being a functional language we allow imperative code we allow mutation but but we encourage you to uh to avoid them as a standard practice scala has been around for quite a while now um we're still working on scholar two i'll i'll be building scala 213.7 on monday actually but in parallel uh work has been going on on scala 3 for a surprisingly long time really the first commit was all the way back in 2012. scholar three became final in may and uh 3-1 just came out last week scala was invented by martin odersky who's uh professor at epfl the technical university in lausanne switzerland and uh there's a whole like consortium of groups uh and and companies that are working on scholar development on the non-profit side at the university uh martin's lab his research lab is called lamp and then he also uh started a non-profit affiliated with a university called the scholar center that does both software development and uh and produces free online courses and sort of oversees the community on the commercial side uh i work at lightbend uh the scholar team at lightbend maintains scala 2 and we've also uh participated in the scholar 3 effort just in the last year or two virtuslab a company in poland has uh stepped up with commercial support for scholar three and they're doing a lot of the work on tooling for scholar three um let's see i've already been introduced i've been at light bend since uh since 2015. the questions that i want to answer in this talk are um why upgrade to straw three um when is a good time to upgrade you know can you do it now and when you do it what's involved how does this go let's start with why upgrade and i i think the the core answer to that is twofold um this is a good upgrade um because uh it's uh an upgrade that i believe we've made as smooth as possible um because scholar 2 installer 3 really share a lot and in the design of scholar 3 there was just enormous emphasis on compatibility and making sure that it was going to be possible for the community and for companies to to do this so much is the same between the two language versions but there's also a really a lot of really nice improvements that make the upgrade appealing so what what is what what hasn't changed what what is shared one of the the biggest things is that we put development of the standard library on hold until this transition was complete scholar 3 uses the scholar 213 standard library as is like it you it is literally the same jar the same bites that will be be on your class path so as you're upgrading you don't have to worry at all about about changes in that area in the language the scala 3 compiler accepts nearly all scholar 2 code either as is or with only uh or with only minor changes what we'll talk a bit about uh what some of the exceptions are but like this is this is really true uh um that uh most code bases can move um without uh uh without a lot of pain and because the two versions share so much uh it has even though scholar threes is pretty new it has been possible to uh already have a really large and healthy library ecosystem and a good set of tooling available for all three so that that that's why this upgrade is smooth for most people uh i'll talk next about why this is an appealing upgrade what are the benefits um i'll talk about particular features of scholar three that i think is appealing but before i get into that i just want to you know really put some emphasis on the the big picture of uh the story behind this upgrade um the the really big reason um to go to a new major version like this um is to put the language on stronger foundations um stronger foundations both theoretically and practically the the new theoretical foundation is the uh dot calculus uh dot for dependent object types um so that is the uh that is the formal mathematical basis for the scholar three type system um and that's been proven sound um but it was also used as the basis for uh for writing the scholar 3 compiler on the practical side um the fact that the compiler is new has uh enabled us to to pay down a lot of the tech debt that accumulated in scholar 2. so uh over the years you know since 2003's a long time um it it's just it had become more and more difficult to um keep scholar 2 going um to to maintain it to fix bugs and to add new features um so a new code base uh for the compiler that like uh you know is based on everything that we learned from our long experience with scala 2 [Music] really means that it's uh uh it's a new lease on life for scholar that that will permit continued improvement and development for a long time yet to come and you'll see some evidence for that if you look at this follow two bud tracker you'll see you'll see a hundred and sixteen open buds um that are still open installed two um but are fixed in scaling three we haven't exhaustively tried to find every such ticket i bet that there are actually a lot more than those 116. so that's some some real evidence that that we're uh seeing over uh overall higher quality with scholar three already in the scholar 3 language design martin sought to take things out as well as to add things so a number of lesser used features are now are now deprecated uh or you know things which were already deprecated in scala ii have been removed as for new features um a lot of the the new features are are not uh completely new um so what one of the design themes with scholar three was to look at how people were actually using uh scala to look at common uh usage patterns or design patterns you might call them and turn those common patterns into built-in language features so a lot of the language features are new at the language level but will already be familiar to scholar 2 programmers one example of that is extension methods in scala 2 extension methods required using implicits in a in a you know very particular way that that that had to be learned they're now supported by scholar 3 directly the old implicit keyword has been replaced by two new keywords given which is the keyword you use at the definition site the the place where you're providing uh implicit values and the keyword at the use site where you require an implicit value is using and a nice uh ergonomic improvement is that it's rarely necessary to to give your implicit names anymore that's now much less commonly needed either at the the definition site or the use site another thing that people used implicits for in scholar 2 was implicit conversions those have been redesigned in scholar 3 to be safer there's less possibility of sort of crosstalk between implicit conversions and other uses of uh of implicits um so but although conversions have been uh redesigned to be safer we're discouraging them uh more and more so if you if you do use them uh plea please do so judiciously something that really is new in the space of implicit is uh is something called context functions um this uh so in scholar two uh uh methods could take implicit parameters but function values could not scholar three lifts that limitation um uh and a function value that that takes an implicit parameters and that is called a context function um martin expects that that this will enable some new programming patterns um that we can't necessarily anticipate yet i don't have time to go into that but i will i would like to recommend one talk video uh which just came out um it's a talk by martin and uh two of his students called representing monads with capabilities uh it makes good use of context functions so other benefits in spell of three scholar three has adopted an indentation syntax that's a type of syntax that you might be familiar with from python or from haskell it allows program structure to be determined not by having to put curly braces around everything but by using an indentation and occasionally an extra extra colon so here's the before and after i want to you know assure everyone that there is no plan to drop the old syntax you're free to continue using the old syntax or to mix them in the same program even within the same file uh although this is the feature of scholar three that that was most controversial in the sense of attracting the most discussion um but i i think we're finding that that as uh even some people uh who are skeptical i would put myself in that camp as initially skeptical uh are really liking this my favorite style three feature for everyday programming is enumerations scholar 3 enumerations support simple use cases like you use in java but they're more powerful and work for more complex things here's an example of a simple use case akin to java this is now more concise to express in scholar three and it also has better java java interop you can say extends java lang and noom and you get something that is easily usable from java but scholar three enumerations also support um uh more complex things so once we're free to use scholar three features in the standard library will redo this uh you know very commonly used class option uh in terms of the noobs they're powerful enough to represent this oh i've had if you see the slashes it's because i'm having to skip slides uh for for reasons of time okay so a major area of improvement in scholar three is in meta programming scala 2 has long supported metaphor meta programming in the form of macros but they were always experimental um even though they were you know they're widely used in scholar 2. we still considered them experimental because they were so hard to write it was hard to write them at all and it was hard to actually get them right so scholar 3 discards the scala 2 macro system entirely and replaces it with a brand new design and it also adds some language features for better programming that means that macros are are needed less often um if you are uh so here's some here's some praise from uh rex kerr a veteran scholar user he says the meta programming uh capabilities of scala three are amazing compared to scholar tube and i think this is actually a really common reaction from people who have actually tried this out that it really is a huge improvement i mentioned that you don't need to actually write macros as often the new language features um that replace many macros one is the new keyword inline scholar two already had inlining there was an inline annotation and an inliner but the inliner was not widely used and the inline annotation was just a hint it was like something you could put in your code and maybe uh what you wrote would get inlined and maybe it wouldn't in scholar 3 when you use the inline keyword inlining is guaranteed so you you're you know for sure that the runtime cost of abstracting something behind an inline definition is zero the type system now includes match types this replaces uh that this is useful for doing type level computations um in scholar two you either had to use macros for that or do very you know tricky and fragile things with uh with implicits also now directly supported is type class derivation that that used to require you know potentially complex and fragile macros okay so that that's a few of my favorite style of three features uh i'm in the slides there's a link to the scholar three documentation page that tells you about more and i would also recommend if you miss them uh watch the videos of the scholar three talks that uh uh from this conference yesterday uh one by dean wanter and another by bill venders both of those were great and and don't overlap you know too much with with this talk okay so hopefully uh i've sort of sold you on the idea of upgrading and uh and you want to know when you can do this uh can you can you do it now i encourage you to be bold on this because uh scala is a statically typed language and that means that the safety level of doing an upgrade like this is uh is uh so much more than it is with a language like python and their famously painful uh two to three upgrade both the type system and the attention to compatibility that's been paid with scholar 3 will help you and protect you but of course there are some reasons that you need to proceed cautiously as with any major technology upgrade it's always good to start small start with a small project start with a greenfield project perhaps start with an internal project rather than starting with live production code if you can in order to build up uh your expertise and experience with scholar 3 before you know embarking on a a much bigger and or more mission critical effort um you'll need to audit your dependencies and and make sure that everything on your class path is something that's still going to work in scholar 3. one of the one of the the biggest uh uh strengths of the scholar three compatible compatibility story is that scholar three can use scala two libraries um and this is such a big step forward from uh what we all remember from scholar 2 where every library had to be rebuilt by its maintainers with every major upgrade so with scholar 3 you don't need to wait for the maintainers to upgrade the library you can just use it there are two big caveats with that one is macros i mentioned that scholar 3 has a brand new macro system so any scholar 2 library that has has macros in it those are are not part of this the maintainers do need to publish those for style 3 before you can use them the other caveat is that you should only use um scholar 2 libraries from scholar 3 applications you should not use scholar 2 libraries from from from other libraries that's because you can run into a diamond uh dependency problem you can mix uh scholar 2 install 3 on the same class path that works fine as we see on the left on the right we see a bad situation we've got into where the author of library d for scholar 3 depended on a scholar 2 library if some other scholar 3 library depends on the scholar 3 version of that same library those will clash you can't have both of those on the same class path it won't work so audit your class path uh for for these problems uh i think most most libraries uh most library authors are uh you know know to avoid this you'll also need to consider all the tooling you're using and and find out whether that tooling is scala 3 ready um ide support for scala 3 is already pretty good intellij works well metals with vs code works well also metals with with some other editors but you'll want to kind of take inventory of all the tooling that that you need i i think you'll be pleasantly surprised at how much other things already already work okay so uh suppose you are ready to upgrade um how does this actually work and i i have a series of little bits of advice on this um first of all um don't try to if you're on an old scholar 2 version don't try to go directly from an old one to three get to schola 213 first then make the jump to three that will really make your uh your life easier if you are cross building which means that you're compiling your code on more than one scholar version um drop drop 212 from your cross build if you can it is definitely possible to cross build against all three to cross build against 212 and 213 and three and actually a lot of the library ecosystem is actually doing this um but if you are free to drop 212 uh from your cross build you you'll really make your life easier um because then you won't have to worry about 212 and 213 having not only different compilers but but different uh different uh standard libraries before you flip the switch and try scott and try scholar 3 on your existing code base it's a good time to look at all the warnings that are coming out of your build and uh and actually pay attention to it so um you know any real-life code base that doesn't have fatal warnings enabled um you know inevitably the the warnings pile up this is this is the time to actually look at those a lot of those warnings have a potential to be to be warning you about things that you were like getting away with in scholar 2 that you're no longer going to be able to get away with in scholar 3. once you've cleaned that stuff up then uh enable this new uh scholar 2 compiler flag called x-source 3 that asks the compiler to produce even more warnings about things that may need attention before you upgrade um i can only give you so much advice about migration that the complete source on this is the scholar migration guide which the scholar center did a really fantastic and really thorough uh job on do read that um and consider using another scholar center project called the scholar 3 migration tool so this is something that will actually attempt to analyze your build and analyze your code and let you know about things that you should change for three once you have flipped the switch and you're running your code through this file 3 compiler there's two compiler flags you should know about one is this dash source 3.0 migration that will ask the compiler to emit additional advice and dash rewrite if you enable it will actually modify your code to be scholar 3 style rather than installer 2 style um i believe that the scala 3 migration tool um also does that um but if for some reason you aren't using that tool you can still use dash rewrite um separately just as a compiler thing you'll need to decide what your point of view is on indentation based syntax i think some teams will stick with the old syntax some teams will convert their entire code bases i think the most typical approach is to move gradually um so this is what the scholar three team are doing themselves with their own code bases is they're just uh they convert to the new syntax when they are actively working on an area of the code and this works because you're free to mix the the uh the two syntaxes you can mix them uh in the same file um even in even within the same class you can mix them um and so i think gradually adopting the the new syntax it works fine and it's it's what i uh what i often see people actually doing i'd like to close now by recommending what i think are the most valuable learning materials for spell 3 um you know at least the ones that i you know that i know about um and that i'm and that i'm confident recommending the scholar center uh in collaboration with 47 degrees has a great series of videos called let's talk about scholar three um there's 12 of them each of them is on a different topic um so you you know they're not like a sequence um so you can pick and choose the topics you that you're interested in the skala center's free online courses have been updated for scholar three some of the courses are classics that they've been running for years that they updated most notably functional programming principles in scala they also made a new course for scholar 3 only called effective programming in scala it's it's another getting started with scholar course but it has i i would contrast it with with functional programming principles by saying that functional programming principles feels more like a university class uh whereas effective programming is more like an on-the-job class it's it's uh uh they uh tried to be extremely practical like like what what is the what are the essentials that i need to know in order to to be productive in scholar 3 at work a number of scholar 3 books are now also available alvin alexander wrote a book that's just called the scholar 3 book that is available for free on the scala documentation site dean wampler updated his classic scholar book for scholar 3

and i wrote the forward to that so that's a favorite of mine i also strongly recommend bill benner's book which he has updated for scholar 3. and then in progress is a scholar 3 version of functional programming in scala which is a really a classic book for for people interested in pure functional programming mike pilquest from type level uh is in the process of updating that for scholar 3 and a preview of that is already available and uh looks like i finished with uh 30 seconds to spare um i will now close zoom and i will head over to the uh spatial chat happy to answer your questions you