In order to understand how Groovy will handle control structures such as if and while , you need to know how it evaluates expressions, which need to have Boolean results. Many of the control structures we examine in this chapter rely on the result of a Boolean test—an expression that is first evaluated and then considered as being either true or false. The outcome of this affects which path is then followed in the code. In Java, the consideration involved is usually trivial, because Java requires the expression to be one resulting in the primitive boolean type to start with. Groovy is more relaxed about this, allowing simpler code at the slight expense of language simplicity. We’ll examine Groovy’s rules for Boolean tests and give some advice to avoid falling into an age-old trap.
Evaluating Boolean tests:
The expression of a Boolean test can be of any (non-void) type. It can apply to any object. Groovy decides whether to consider the expression as being true or false by applying the rules shown in table 6.1, based on the result’s runtime type. The rules are applied in the order given, and once a rule matches, it completely determines the result.
Listing 6.1 shows these rules in action, using the Boolean negation operator ! to assert that expressions which ought to evaluate to false really do so.
- Listing 6.1 Example Boolean test evaluations
- // Boolean values are trivial
- assert true
- assert !false
- // Matchers must match
- assert ('a' =~ /./)
- assert !('a' =~ /b/)
- // Collections must be non-empty
- assert [1]
- assert ![]
- // Maps must be non-empty
- assert ['a':1]
- assert ![:]
- // Strings must be non-empty
- assert 'a'
- assert !''
- // Numbers (any type) must be nonzero
- assert 1
- assert 1.1
- assert 1.2f
- assert 1.3g
- assert 2L
- assert 3G
- assert !0
- // Any other value must be non-null
- assert new Object()
- assert !null
Assignments within Boolean tests:
Before we get into the meat of the chapter, we have a warning to point out. Just like Java, Groovy allows the expression used for a Boolean test to be an assignment—and the value of an assignment expression is the value assigned. Unlike Java, the type of a Boolean test is not restricted to boolean, which means that a problem you might have thought was ancient history reappears, albeit in an alleviated manner. Namely, an equality operator == incorrectly entered as an assignment operator = is valid code with a drastically different effect than the intended one. Groovy shields you from falling into this trap for the most common appearance of this error: when it’s used as a top-level expression in an if statement. However, it can still arise in less usual cases.
Listing 6.2 leads you through some typical variations of this topic.
- Listing 6.2 What happens when == is mistyped as =
- def x = 1
- if (x == 2) {
- assert false
- }
- /*******************
- if (x = 2) {
- println x
- }
- ********************/
- if ((x = 3)) {
- println x
- }
- assert x == 3
- def store = []
- while (x = x - 1) {
- store << x
- }
- assert store == [2, 1]
- while (x = 1) {
- println x
- break
- }
- while (1 = x) { // Should be ==
- println x
- }
沒有留言:
張貼留言