Scala: pass-by-value vs pass-by-name

pass-by-value: expressions as arguments to a function are evaluated once on passing to the function

pass-by-name: expressions are evaluated only when referenced, if they are not referenced they are not evaluated, and the expression is evaluated every time the expression is referenced

System.out.println("Calling squareCallByValue(): ")
squareCallByValue( doSomething() )

System.out.println("nCalling squareCallByName(): ")
squareCallByName( doSomething() )

System.out.println("nCalling callByNameNoReference(): ")
callByNameNoReference( doSomething() )
def doSomething() : Int = {
    System.out.println("doSomething() called")

    2
  }

  def squareCallByName(x: => Int) = {
    var result = x * x
    System.out.println("x in squareCallByName: " + result)
  }

  def callByNameNoReference(x: => Int) = {
  }

  def squareCallByValue(x: Int) = {
    var result = x * x
    System.out.println("x in squareCallByValue: " + result)
  }

Scala IDE: Scala Library error on a new project

Created my first project in Scala IDE and got this error on my project:

"Unable to find a scala library. Please add the scala container
or a scala library jar to the build path."

It seems that the project build path is missing the Scala system library. To fix:

– right-click project, click Properties

– click Java Build Path, then Libraries tab

– press Add Library button, select ‘Scala Library’ and then ok.

Done!

Scala notes: basic functions

A few notes for myself so I don’t forget:

A function like:

f(x) = x * x

is declared in Scala like:

def square(x : Int) = x * x

… which is a function called square, which takes a value x which is of type Int, and calculates the square of x.

Rod Johnson joins Typesafe

Rod Johnson, founder of the Spring Framework and the SpringSource company, left Vmware/SpringSource a few weeks back and has resurfaced with Typesafe, founded by the developers of the Scala language.

It will be interesting to see in the coming months whether Johnson can add credibility to Scala to help enterprise adoption of the language.