Udemy

12 - Functional Style and try...catch...finally

A free video tutorial from Dick Wall
Scala Developer
Rating: 4.5 out of 5Instructor rating
8 courses
39,900 students
12 - Functional Style and try...catch...finally

Learn more from the full course

Scala Applied, Part 1

Introduction to programming in the Scala language. Core syntax and concepts.

05:44:10 of on-demand video • Updated March 2023

Use the REPL (the Scala Interactive Shell) for experimentation and fun
Understand the basics of Scala syntax, including val, var, def, if, while, try, for and more
Create classes, objects and instances
Define and use function literals and higher order functions
Understand the basics of Scala's type inference and how to use it
Write custom control structures to augment those built into the language
Start down the path of a more functional style of programming
Be ready for Part 2 of Stairway to Scala Applied, which will dig into some of the differences between Scala and other languages
English [Auto]
At this point, I'm going to switch over to using something we're going to see in the next module, which is the worksheet feature in IntelliJ. Just because it's a little bit quicker than watching me type all of these examples in. So in IntelliJ, if I switch to that now, you'll see that I have created a method called Max squared double that looks very similar to that example that I just showed you. And it does it, this takes two parameters. Again, we have to provide the, the types for these parameters because Scala can't infer them. We don't have to provide the return type, but often that's considered good practice to put that in there anyway. So we take a event and B event and then we say if A is greater than B and then this does the val squared equals A times A and squared times two. So again, squared times two will be what the result of this particular curly brace block is. And if, if B is then bigger than or equal to A, then we take B times B and return the value of that squared. And that's implicitly returned from that block as well. So when we call it down here and you can run the the worksheets here, you'll actually get these or you have these examples from unpacking the the projects and we'll see how to import those in the next in the next module. But when you when you've got these, you can actually make them run. So evaluate the worksheet and I'll do that here and you'll see it run through and down the side here, you can see that indeed when we call Max Square double with five and three, five is the greater one of the two. So it does five times five, which is 25, and then multiplies it by two, which gives us 50. If I change this to seven and actually let's change it the other way, let's do five and seven. So we've got B as the larger and then I save that. You should see it reevaluate here. If not, we can force it. Maybe I've got that turned off, but you can always force it to reevaluate and when it reevaluates this time it comes back with 98. So that did seven times seven squared, which is 49 and then multiplied that by two and that gives us 98. So the another thing that Scala makes or you know, traditionally in other languages is often a statement. But in Scala is an expression is the try catch finally construct. And so this means that when we do a try with a catch and a finally whatever is in the try block, if that works, that will be returned as the value. Okay, so here we can do ten divided by zero. Now in integers that will result in an arithmetic exception. So we can catch that and we do the catch with this construct. We say catch open, curly brace, and then we provide a sequence of case or a series of cases that will hopefully try and match the exceptions that we're getting. And in this example, we're going to get an arithmetic exception. So at this point, we use this rocket. We're going to see all about the rocket syntax later. But it basically means when this happens, do this. And what we use here is just a zero. So in the case of a catch, the result of this expression will be assigned to divide it, and the type that's inferred will be a combination of these two. We'll see all about that type inference later. The finally block is always run. But although we have a 42 at the end here, that will not affect the result. The finally block is always run, but it only has a side effect. It will never affect the actual result returned from this. So if I go back to my Repl here and you'll see this, here's my example of divided. I can put the divided try A and B -12 and I think B was set to 12. So that will cause an arithmetic exception. I can also put a finally block in here and just say println. Uh, whoops. This is a side effect and I can return 100 from here, and that 100 will not come back as a result of this. It will still be zero. And that's because this one failed. So arithmetic exception is the result of this. And that returns a zero if this is able to work. In other words, if I change this to 11, instead you'll see this get re-evaluated. And now the result is ten divided by one is ten. So hopefully that makes sense to you. Again, there's nothing here that stops us from ignoring the return type, in which case we turn try effectively back into a statement again. But as you can see in Scala, we have the option of making things expressions and assigning them to Vals, which means that we can be more functional in our programming approach, and that's generally considered to be good practice.