Thursday, January 28, 2016

Scala Getter/Setter and BeanProperties

Scala provides automatic getter/setter functions for class data members After compiling this piece of code using Scala compiler scalac
scalac Foo.scala
Generates Foo.class file which we can disassemble using Java class file disassembler program javap https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javap.html
javap -private Foo.class
Disassembling class file shows
public class Foo {
  private int foo;
  public int foo();
  public void foo_$eq(int);
  public Foo();
}
Here we can see int foo() returns foo value and foo_$eq(int) assigns the value to private int foo (works like a setter). Which means we do not have to define private data member, getter and setter explicitly like;
private var _foo: Int = _
def foo_=(foo: Int) = _foo = foo    // setter
def foo = _foo    // getter
Instead, we can only declare
var foo: Int = _
On the other hand, Scala provides an annotation @BeanProperty which you can use to declare automatic getter/setter functions. To define a data member as a bean property you just have to write @BeanProperty before declaring member variable
import scala.reflect.BeanProperty
@BeanProperty var foo: Int = _
This should be implemented when you call Scala code from Java. Java libraries and APIs expect a class getter/setter function names as getMember/setMember. @BeanProperty generates these getter/setter functions too. That is why it is feasible to use @BeanProperty instead of simple public variable declaration when intermixing Java with Scala.

Monday, January 25, 2016

Playing with Scala - 1

Being a programmer, whats point in not learning a language that is a bit complex than others?

Being Scala developer from almost 4 months now. I admire its compact logic building and strong syntactical shortcuts. I didn't dive much into its functional part, but I would like to share some of it also.
  1. It has so many flavors; programmers having background in C, C++, Java, Python get easily familiar with its syntax
  2. Object-oriented functional language; unlike Haskell and LISP - that are purely functional and Java - that is purely object oriented language, Scala is everything! All in one
  3. Salient features of Scala are nicely described here http://www.scala-lang.org/what-is-scala.html
  4. Runs on JVM so Java classes in Scala can be freely mixed
  5. Highly scalable; works from your desktop PC to a thousand nodes cluster i-e; Intel, Twitter and LinkedIn use Scala in their clusters
  6. Unlike Kotlin and Ceylon programming languages, Scala decided to make "better Java" instead of staying close to Java and ultimately improving almost nothing in it except syntactic sugar

Monday, January 11, 2016

A Few Lessons Learned From 2015

I tried to sum up my mistakes I did last year. Some of them were pretty dumb, others made me embarrassed to the point I took decision to never repeat them ever. Some of them made me proud. Some of them made me think what do I have to be proud of? Some of the key points, I learned, are mentioned below;
  1. Being yourself doesn't hurt you
  2. Hurting people hurts you eventually
  3. Don't sleep too much
  4. Productivity increases late-night (after 1am)
  5. Trustworthy people never ask you to trust them
  6. People who want to be in your life wouldn't stay shut about it
  7. Don't focus on peoples' interests more than you feel like you should
  8. Work quietly and let your work speak
  9. Read books in isolation and feel them
  10. Stay sharp and ready for attacks other frame for you
  11. Don't get framed in others' problems, be smart to judge your surroundings
  12. Don't offer to help if you can't do it honestly
  13. Don't listen to those who want to be listened and never listen to you
  14. Don't be gender biased in public or group talks
  15. Select a few words and make up your wit-dictionary and use it over and over again till you make habit of staying away from slang
  16. Dislike what you don't like but never dislike what others want you to dislike. There is always something beneficial for you what others suggest you to dislike
  17. Don't talk unless you have a strong reason
  18. Don't change yourself to comfort others