Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Scala name parameter by-name parameter

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use the Scala naming parameter by-name parameter". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The withPrintWriter method requires a parameter of type PrintWriter. This parameter is displayed as "writer = >":

WithPrintWriter (file) {writer = > writer.println (new java.util.Date)}

However, what if you want to implement something more like if or while and have no value for passing code between curly braces? To address this situation, Scala provides a name parameter.

To give a practical example, imagine that you need to implement an assertion architecture called myAssert. You can only call it myAssert, not assert, because Scala provides its own assert, which will be described in Section 14. 1. The myAssert function takes a function value as input and references a flag bit to determine what to do. If the flag bit is set, myAssert will call the incoming function and verify that it returns true. If the flag bit is turned off, myAssert will quietly do nothing.

If you don't have a name parameter, you can write myAssert like this:

Var assertionsEnabled = true def myAssert (predicate: () = > Boolean) = if (assertionsEnabled & &! predicate ()) throw new AssertionError

This definition is correct, but it can be a bit ugly to use:

MyAssert (() = > 5 > 3)

You may want to omit the empty argument list and the = > symbol in the function text and write it in the following form:

MyAssert (5 > 3) / / will not be valid because it is missing () = >

The alias function appears just to fulfill your wish. To implement a naming function, you define the type of parameter that starts with = > instead of () = >. For example, you can change the predicate parameter of myAssert to a name parameter by changing its type, "() = > Boolean" to "= > Boolean". Code 9.5 shows what it looks like:

Def byNameAssert (predicate: = > Boolean) = if (assertionsEnabled & &! predicate) throw new AssertionError

Code 9.5 uses the alias parameter

Now you can omit the empty parameters in the properties that need to be asserted. The result of using byNameAssert looks as if you used a built-in control structure:

ByNameAssert (5 > 3)

In the filename type, the empty parameter list, (), is omitted and is only allowed in the parameter. There is no such thing as a name variable or name field.

Now, you may wonder why you can't simplify the writing of myAssert, using the stale Boolean as its parameter type, such as:

Def boolAssert (predicate: Boolean) = if (assertionsEnabled & &! predicate) throw new AssertionError

Of course, this format is also legal, and the code that uses this version of boolAssert still looks the same:

BoolAssert (5 > 3)

Nevertheless, there is a very important difference between the two approaches to be noted. Because the parameter type of boolAssert is Boolean, the parenthesized expression in boolAssert (5 > 3) is evaluated before the call to boolAssert. The expression 5 > 3 produces true, which is passed to boolAssert. On the other hand, because the predicate parameter of byNameAssert is of type = > Boolean,byNameAssert (5 > 3), the expression in parentheses is not evaluated before the call to byNameAssert. Instead, create a function value whose apply method evaluates 5 > 3, and that function value is passed to byNameAssert.

So the difference between the two approaches is that if the assertion is disabled, you will see some side effects of the expression in parentheses in boolAssert, while byNameAssert does not. For example, if an assertion is disabled, an attempt to assert "x / 0 = = 0" in boolAssert's example will result in an exception:

Scala > var assertionsEnabled = false assertionsEnabled: Boolean = false scala > boolAssert (x / 0 = = 0) java.lang.ArithmeticException: / by zero at.

< init>

(

< console>

8) at.

< clinit>

(

< console>

) at RequestResult$.

< init>

(

< console>

3) at RequestResult$.

< clinit>

(

< console>

).

But trying the same code assertion in the byNameAssert example will not generate an exception:

Scala > byNameAssert (x / 0 = = 0) "how to use the Scala name parameter by-name parameter" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report