In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the operator of Scala". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Scala operator.
Scala provides a rich set of operators for its basic types. These operators are really just fancy syntax for ordinary method calls. For example, 1 + 2 and (1). + (2) are actually the same thing. In other words, the Int class contains a method called +, which takes an Int parameter and returns an Int result. This + method is called when two Int are added:
Scala > val sum = 1 + 2 / / Scala called (1). + (2) sum: Int = 3
To prove this, you can explicitly write the expression as a method call:
Scala > val sumMore = 1). + (2) sumMore: Int = 3
The real truth is that Int contains many overloads with different parameter types: the + method of overload. Overloaded methods have the same name and different parameter types. For example, Int has another method parameter also called + and the return type is Long. If you add Long to Int, the replacement + method will be called:
Scala > val longSum = 1 + 2L / / Scala called (1). + (2L) longSum: Long = 3
The symbol + is the operator-or more specifically, the infix operator. Operator tagging is not limited to things that look like operators in other languages like +. You can label any method as an operator. For example, the class String has a method indexOf that takes a Char parameter. The indexOf method searches for the specified character that appears * times in String and returns its index or-1 if it is not found. You can use indexOf as an infix operator, like this:
Scala > val s = "Hello, world!" S: java.lang.String = Hello, world! Scala > s indexOf'o' / / Scala called s.indexOf ('o') res0: Int = 4
In addition, String provides an overloaded indexOf method with two parameters, the character to search for and the index from which to start the search. The previous indexOf method starts with index zero, where String starts. Although this indexOf method takes two parameters, you can still use it as an operator annotation. However, when you use operator annotation to call a method with multiple parameters, the parameters must be placed in parentheses. For example, here is an example of how to use another form of indexOf as an operator (following the previous example):
Scala > s indexOf ('oaked, 5) / / Scala called s.indexOf (' oiled, 5) res1: Int = 8
Any method can be an operator
Operators in Scala are not special language syntax: any method can be an operator. Use the method to make it an operator. If written as s.indexOf ('o'), indexOf is not an operator. But if it is written as s indexOf'okeeper, then indexOf is the operator, because you use it as operator tagging.
So far, you have seen examples of infix: infix operator tagging, which means that the called method is between the object and the parameters or parameters passed to the method, such as "7 + 2". Scala also has two other operator annotations: prefixes and suffixes. In prefix annotations, the method name is placed before the called object, such as'-'in-7. In suffix annotations, the method is placed after the object, such as "toLong" in "7 toLong".
In contrast to the infix operator-- the operator takes the last two operands, one on the left and one on the right-- in contrast, the prefix and suffix operators are unary: unary: they take only one Operand. In prefix mode, the Operand is to the right of the operator. Examples of prefix operators are-2.0 and ~ 0xFF. Consistent with infix operators, these prefix operators are shorthand ways to call methods on value type objects. In this case, however, the method name is prefixed with "unary_" on the operator character. For example, Scala converts the expression-2. 0 into a method call "(2. 0). Unary_-". You can demonstrate this by entering a call to a method through both an operator and an explicit method name:
Scala >-2. 0 / / Scala called. Unary_- res2: Double =-2. 0 scala >. Unary_- res3: Double =-2. 0
The only identifiers that can be used as prefix operators are +, -! And ~. So, if you define the name unarykeeper! You can call this method with a prefix operator on the appropriate type value or variable, like! But if you define a method called unary_*, you can't use it as a prefix operator because * is not one of the four identifiers that can be used as a prefix operator. You can call it as usual, such as p.unarykeeper, but if you try to call it like * p, Scala will understand it as * .p, which may not be taken for granted! However, it is not hopeless. There is still a slim chance that your program with * p may be compiled like C++.
Suffix operators are methods that are called with no points or parentheses. In Scala, you can discard the empty parentheses for method calls. The exception is to add parentheses if the method has side effects, such as println (), but remove the parentheses if the method has no side effects, such as toLowerCase called on String:
Scala > val s = "Hello, world!" S: java.lang.String = Hello, world! Scala > s.toLowerCase res4: java.lang.String = hello, world!
In the later example, the method has no parameters, or you can drop the point and use the suffix operator to mark it:
Scala > s toLowerCase res5: java.lang.String = hello, world!
In the example, toLowerCase is treated as a suffix operator on the Operand s.
So to know which operators you can use in Scala's value type, all you need to do is query the methods defined on the value type in Scala's API document.
At this point, I believe you have a deeper understanding of "how to use the operator of Scala". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.