Показаны сообщения с ярлыком fp. Показать все сообщения
Показаны сообщения с ярлыком fp. Показать все сообщения

понедельник, 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.

пятница, 2 ноября 2012 г.

Typed approach for object ids in Scala

Short example

Try to imagine that you are writing some application in Scala, use case classes to store your data, and something like Salat to automatically serialize your classes. Occasionly, you have two objects linked together:

At some point at time you need to add another link with another object:

Hey, now it compiles, but doesn't work! If you have tests, we can say you are in luck: you can run tests, try to fix broken line, run tests another time, try to fix other line... No fun at all. Or if you don't cover some method, you can have time bomb somewhere. Worst case ever!


Type system to the rescue!

What is type system? A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kind of values they compute. In other words, type system allows compiler to prove correct behavior of you program! So what is fun writing in language with powerful type system but don't use it powers?

At this point we already have some amount of code and tons of data, so we don't want to break serialization. Also, we don't want additional boxing for every id. What can we do here? Haskell, the Language of Types, already has a concept for this! It is called newtype. Using it, you can introduce new type on top of other, and have conversions from one to another, but without additional boxing -- values of source type and defined newtype will be represented identically in memory, so from one side you will not loose memory and cpu time for useless boxing, and Salat will serialize your data like it was old Keys!

And of course, this is in some Scala libraries for free! shapeless is one of them. Here goes an example: Using it, we can define type, representing object's id:

So our first example will become: And from this moment, we can change our definitions as we want, with compile-time check!