For Java™ developers who learn Scala, objects are a relatively natural, simple entry point. In previous installments of this series, I introduced some of the object-oriented programming methods in Scala, which are actually not very different from Java programming. I also showed you how Scala can reapply the traditional object-oriented concept, find its flaws, and reinvent it in the light of 21st century's new requirements. Some of the important things Scala has been hiding are going to come out: Scala is also a functional language (where the function is relative to other Dys function languages).
Scala's function-oriented nature is well worth exploring, not only because the object content has been studied. Functional programming in Scala will provide some new design structures and ideas, as well as some built-in constructs that make programming of certain scenarios (such as concurrency) very simple.
This month, for the first time, you will enter Scala's functional programming domain to see Four types that are common in most functional languages: Lists (list), tuples (tuple), sets (set), and Option types. You'll also learn about Scala's arrays, which are new to other functional languages. These types all suggest a new way to write code. When combined with traditional object-oriented features, very concise results can be generated.
Using Option (s)
Under what circumstances does "none" mean "nothing"? When it is 0, what is the relationship to null?
For the concept that most of us are very familiar with, it is very difficult to say "none" in software. For example, look at the intense discussions around null and 0 in the C + + community, or the debate over the null column values in the SQL community, to be aware of one or two. Null or null represents "none" for most programmers, but this raises some special questions in the Java language.
Consider a simple operation that can look up the salary of a programmer from some database located in memory or disk: The API allows the caller to pass in a String containing the programmer's name, what does it return? From a modeling standpoint, it should return an Int, representing the programmer's annual salary; But there is a problem here, if the programmer is not in the database (probably not hiring her at all, or has been fired, or loses the wrong name ...). ), then what should be returned. If the return type is INT, then null cannot be returned, and this "flag" usually means that the user is not found in the database (you might think you should throw an exception, but most of the time the database loss value is not considered an exception and should not be thrown here).
In Java code, we eventually mark the method as returning Java.lang.Integer, which forces the caller to know that the method can return null. Naturally, we can rely on programmers to fully archive this scenario, and we can rely on programmers to read carefully prepared documents. This is similar to the following: We can ask the manager to listen to our objections to the impossible deadlines they have requested, and then the manager will further communicate our objections to our superiors and users.
Scala provides a common functional approach to breaking the deadlock. In some respects, Option type or option[t], the description is not valued. It is a generic class with two subclass Some[t] and None to represent the possibility of "no value" without the need for a language type system to support this concept with great difficulty. In fact, using the option[t] type can make the problem clearer (the next section will use).
When using option[t], the key point is to realize that it is essentially a strongly typed collection of size "1", using a different value of None to represent the possibility of a "nothing" value. Therefore, here the method does not return null to indicate that the data is not found, but rather declares it to return option[t], where T is the original type returned. So, for a scenario where no data is found, simply return none, as follows:
Listing 1. Are you ready to play football?
@Test def simpleOptionTest =
{
val footballTeamsAFCEast =
Map("New England" -> "Patriots",
"New York" -> "Jets",
"Buffalo" -> "Bills",
"Miami" -> "Dolphins",
"Los Angeles" -> null)
assertEquals(footballTeamsAFCEast.get("Miami"), Some("Dolphins"))
assertEquals(footballTeamsAFCEast.get("Miami").get(), "Dolphins")
assertEquals(footballTeamsAFCEast.get("Los Angeles"), Some(null))
assertEquals(footballTeamsAFCEast.get("Sacramento"), None)
}