понедельник, 25 февраля 2013 г.

Initializing vars in scala

Explanation

One common question I hear very often is "what is the difference between x = null and x = _"? Let us try to see what it does. Consider example: Here we see, how it get compiled, using "javap -c". At first, let check out null-example: Obviously, it will assign null to x field. Now, compare with second example: Code is much shorter and all it does is just calling for parent constructor. Note, that field x is not initialized at all! So, correct answer is: difference between standard and underscore initializers is that underscore initializers does not set any field value in constructor.

When you should care

Consider you are using Selenium with field annotations and PageObject pattern, writing something like that: Suddenly, you will notice that your button object is null. What is going on, I've typed all necessary letters! you would say. Explanation is simple: magic fields are initialized during parent PageObject constructor, but after that, FooPage constructor will erase values you wanted. Correct solution is to use underscore initializer here.

Комментариев нет:

Отправить комментарий