Crisp Reading Notes on Latest Technology Trends and Basics

Scala avoids JavaBeans code

One of the most powerful features of Scala is that it removes the highly repetitive nature of writing Java Beans (something that occupies major space in programs and books)

Constructors and Accessors – Motivating Example

The Scala way

To code a class BankAccount with

  • A immutable string – name
  • A mutable int – balance
class Account( val name: String, var balance: int )
acc = new Account(“scott”, 25)
println(acc.name)
println(acc.balance)

The Java way

The equivalent code in Java would have

  • A constructor
  • A getter only for the immutable entity
  • A getter and setter for the mutable entities
public class Account {
  private String name;
  private int balance;

  public Account(String name, int balance) {
    this.name = name;
    this.balance = balance;
  }

  public String getName() {
    return this.name;
  }

  public int getBalance() {
    return this.balance;
  }

  public void setBalance(int balance) {
    this.balance = balance;
  }
}

IDEs do this work – Why bother

IDEs still produce code that you need to maintain. It could be distracting.

Also, if there is a way to modify the generated code, there will be a temptation to modify it. After that, change management becomes hard.

The meaning of the words on the constructors

  • If a constructor argument is specified as val, then only a getter is generated for it
  • If a constructor argument is specified as var, then both a getter and setter are generated for it
  • If a constructor argument is specified as neither, then, the variable is not part of the class. The argument is used in the constructor and discarded.

Multiple and Custom Constructors

Like Java, Scala allows multiple and custom constructors. It also improves by having only one primary constructor and having secondary constructors go through the same

Multiple Constructors

Like Java, scala allows multiple constructors. It calls one constructor as primary, and the other constructors as secondary

If the bank accounts were opened with zero balance, the most common case, a new constructor could be used

class Account( val name: String, var balance: int ) {
  def this( val name: String ) = {
    this( name, 0 );
  }
}

Custom Code in Primary Constructors

Primary constructors also have custom code in them

Suppose our constructor took a SSN, and did a lookup for the name

class Account(val ssn: int, var balance int) {
  val name = ssn_lookup(ssn)

  def ssn_lookup(val ssn: int): String {
    // To be implemented
  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: